%%                MTEX Workshop ICOTOM 2017
%                     
%%                  EBSD Analysis with MTEX
%            Ralf Hielscher, TU Chemnitz, Germany
%            David Mainprice, Geosciences Montpellier, France
%
%%
% The following script demostrates some of the facilities of MTEX to
% import, correct and analyze EBSD data.
%
% Run this script section by section and follow the output. You are
% encauraged to alter the script to get a better feeling about MTEX.
%
% important shortcuts:
% Strg + Return         - run the current section
% Shift + Strg + Return - run the current section and go to the next one
% F9                    - evaluate the selected command
%

% import the ebsd data
ebsd = loadEBSD('Emsland_plessite_500x_15_DS.ctf')

% the involved symmetries
cs_bcc = ebsd('Fe').CS
cs_aus = ebsd('Aus').CS

%% displaying the phase

% some conventions
plotx2east
plotzOutOfPlane

% plot the present phases
figure(1)
plot(ebsd)

%% display the orientations in pole figures
%

% extract orientations
% important: orientations can be extracted only for one phase
ori = ebsd('fe').orientations

% which crystal planes
h = Miller({1,0,0},{1,1,0},cs_bcc);

figure(2) % plot in a new window
plotPDF(ori,h)

%% rotate EBSD data
% it is important to unerstand the difference between
% 1 - rotating view
% 2 - rotating the spatial coordinates
% 3 - rotating the Euler angles

ebsd_rotated = rotate(ebsd,10*degree,'keepEuler')

figure(1)
plot(ebsd_rotated,'micronbar','off','figSize','huge')
figure(2) % plot in a new window
plotPDF(ebsd_rotated('fe').orientations,h)


%% colorize EBSD data according to its orientation
% important: only one phase can be colorized in one command

plot(ebsd('Fe'),ebsd('Fe').orientations,'figSize','large')

% what is going on here?

%% explicite orientation coloring

% define an explicite map for converting orientations into colors
oM = ipdfHSVOrientationMapping(cs_bcc)

% understand the difference to
% oM = ipdfHSVOrientationMapping(cs_bcc.properGroup)

% this map has many parameters which can be set
oM.inversePoleFigureDirection = xvector;

figure(2)
plot(oM)

%%

% transform orientations into a list of colors
color = oM.orientation2color(ebsd('Fe').orientations);

% plot the ebsd data set
figure(1)
plot(ebsd('Fe'),color,'figSize','huge')

%% plot the orientations into the color key

figure(2)
hold on
plot(ori,'MarkerFaceColor','none','MarkerEdgeColor','black')
hold off

%% implicitely plot a contoured inverse pole figure

figure(3)
plotIPDF(ori, oM.inversePoleFigureDirection, 'contourf')
mtexColorbar
% this takes a while since it considers all orientations
% but allows us to talk what is happening in behind

%% explicitely plot a contoured inverse pole figure

odf = calcODF(ori,'halfwidth',5*degree)

figure(4)
plotIPDF(odf, oM.inversePoleFigureDirection,'contourf')
mtexColorbar

%% some chemical information

% import chemical information from a txt file
ni = txt2mat('ARGUS_Ni.txt');

% plot them along with the data
plot(ebsd,ni)


%% add the additional information to the ebsd variable

% turn the data set into rectangular shape
ebsd = ebsd.gridify;

% include the chemical information into the ebsd variable
ebsd.prop.ni = ni;

% plot the chemical information
plot(ebsd,ebsd.ni)

%%

h = Miller({1,0,0},{1,0,1},cs_bcc);
plotPDF(ebsd('fe').orientations,ebsd('fe').ni,h,'points',500)
mtexColorbar
CLim(gcm,[0 20])

%% compute grains

[grains,ebsd.grainId] = calcGrains(ebsd('indexed'))

grains = smooth(grains,5)

%% overlay chemical information with grain structure

plot(ebsd,ebsd.ni)
hold on
plot(grains.boundary)
hold off

%% check for correlations between Ni value and band contrast

clf
scatter(log(ebsd.ni(:)), ebsd.bc(:))
