%
%
%                  /$$$$$$$$ /$$$$$$$   /$$$$$$  /$$$$$$$                       
%                 | $$_____/| $$__  $$ /$$__  $$| $$__  $$                      
%                 | $$      | $$  \ $$| $$  \__/| $$  \ $$                      
%                 | $$$$$   | $$$$$$$ |  $$$$$$ | $$  | $$                      
%                 | $$__/   | $$__  $$ \____  $$| $$  | $$                      
%                 | $$      | $$  \ $$ /$$  \ $$| $$  | $$                      
%                 | $$$$$$$$| $$$$$$$/|  $$$$$$/| $$$$$$$/                      
%                 |________/|_______/  \______/ |_______/                       
% 
% 
% 
%                                                        /$$                    
%                                                       |__/                    
%       /$$$$$$  /$$   /$$  /$$$$$$   /$$$$$$   /$$$$$$$ /$$  /$$$$$$$  /$$$$$$ 
%      /$$__  $$|  $$ /$$/ /$$__  $$ /$$__  $$ /$$_____/| $$ /$$_____/ /$$__  $$
%     | $$$$$$$$ \  $$$$/ | $$$$$$$$| $$  \__/| $$      | $$|  $$$$$$ | $$$$$$$$
%     | $$_____/  >$$  $$ | $$_____/| $$      | $$      | $$ \____  $$| $$_____/
%     |  $$$$$$$ /$$/\  $$|  $$$$$$$| $$      |  $$$$$$$| $$ /$$$$$$$/|  $$$$$$$
%      \_______/|__/  \__/ \_______/|__/       \_______/|__/|_______/  \_______/
% 
% 
% The aim of this exercise is to become more familiar with ebsd data using
% mtex and addressing and plotting properties.

% Quite often, you can find help by typing `help` command (without the ``).
% If one command might be available for different
% data types, it should be indicated at the bottom of the help. Alternatively,
% try e.g. `help orientation/calcMDF` or `help odf/calcMDF`


%% 0) load some data -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% change directory to the place where the sample data is located
% cd workshopfolder

% we will use a cropped part of a larger ebsd map, hence at the moment we
% will not use the import_wizard for now
load ebsd_workshop.mat

% the crystal symmetry as it would be produced by the import_wizard
% template
% CS = {... 
%   'notIndexed',...
%   crystalSymmetry('-3m1', [4.913 4.913 5.501], 'X||a*', 'Y||b', 'Z||c', 'mineral', 'Quartz', 'color', 'purple')};
% this dataset originates from an hkl/oxford system, hence X||a*,
% originally they set the crystalSymmetry to be -3m and the c-axis as 5.5,
% however it should be 321 and 5.401, we will change this later
% % we can retrieve the crystal symmetry as stored in the dataset from the ebsd 
% CS = ebsd.CSList

% plotting convention
setMTEXpref('xAxisDirection','east');
setMTEXpref('zAxisDirection','intoPlane');


%% 1) explore the ebsd data *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% hint: using ebsd.[TAB] will show you some properties and functions
% available for the ebsd object
% a) Which properties does it have, how big is it (in pixel, in micron), ...
% hint: have a look at ebsd.x/y and ebsd.unitCell        
        
% -------------------------------------------------------------------------
% b) What is the cystal symmetry, which parameters are used?


% Quartz is trigonal with a symmetry of '321', and cell parameters of a,b= 4.913 c= 5.405
% Oxford/HKL decides decided to use -3m, is that a proper group?   


% Can we change the symmetry to a proper group?       


% The c-axis should be 5.401, define the correct cs with a correct correct
% c-length


% -------------------------------------------------------------------------        
% c) What can you say about the quality of the data?
%       mad = mean angular deviation:misfit between detected bands and
%       simulation (in degree, high mad = 'bad' solution)
%       bc = band contrast: ratio of actual intensity in hough space vs
%       average intensity of kikuchi bands (high value = high contrast)
%       bs =band slope : higest intensity gradient at the morgin of kikuchi
%       bands (high value = sharp band)
%   -  What is the mean/min/max quartz mad?

%   -  What is the percentage of pixels with a less reliable indexing?
        % query points with a specific condition e.g. % of pixels with mad > 1 
        
        
%   -  Is there any relation between mad and band contrast?


%% % 2) plot properties in a map -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% a) Colorcode the number of bands used for indexing in a grey-scale color-coding
% and non-indexed pixels in red
% hint: plotting ebsd data in mtex works in almost all cases like plot(ebsd,property)
% while ebsd and property obviously need to have the same length


% -------------------------------------------------------------------------
% b) plot the band contrast and plot band contrast lower 50 as black
% hint: you could either plot two maps on top of each other using hold
% on/off, you can set the colorrange using CLim and you can get the
% colormap cm=colormap of the current plot and maniplulate the values
% hint: if you just want to plot those pixels with a specific condition,
% use logical indexing: e.g. a=1:9; a(a > 4) returns 5 6 7 8 9
% ebsd data can be querried logical indexing



% -------------------------------------------------------------------------
% c) many types of data be colorcoded e.g.
% colorcode all quartz points in the dataset as a function of distance from
% an arbitrary line
% hint: distance from a line defined by two points, 
% e.g. http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
% using [pq]= ginput(2) will let you select two points on a map
% this data can also be added to the ebsd dataset by 
% ebsd.prop.distancefromline =distancefromline


%

%% 3) orientations -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

% a) access orientations and the first 100 Euler angles


% ------------------------------------------------------------------------- 
% b) display orientations as polefigure plot for c (0,0,0,1) and a
%  (1,1,-2,0), learn that orientation*Miller gives a vector in sample
%  reference which can be plotted using plot() (see also help vector3d/plot)
%  and also use plotPDF() (see help orientation/plotPDF)
% 

%define Millers

% option 1:  derive vectors for the axis direction


% option 2: use plotPDF


% -------------------------------------------------------------------------
% c) plot orientaions as sigma sections
% hint: have a look at help plotSection


% -------------------------------------------------------------------------
% d) plot orientations in an inverse pole figure wrt to x,z and a direction 
% oriented NW-SE 
% option 1: learn that orientation\vector or inv(o)*vector gives a Miller
% and option 2, use plotIDPF (see help orientation/plotIPDF)

% option1: derive the miller parallel to specimen direction
dir=[xvector zvector vector3d(1,1,0)] % these would be the directions

% option 2: use plotIPDF


% -------------------------------------------------------------------------
% e) do pixels indexed with just few bands (e.g. <=6) show a different 
% pf than those with many (e.g. >9) ? Color-code each point according to
% the mad
% hint: use logical indexing: e.g. a=1:9; a(a > 4) returns 5 6 7 8 9
% ebsd data can be querried logical indexing
% another hint: a second arument in plot/plotPDF/plotIPDF might be any data
% array which is used for colorcoding


%% 4) orientation/direction/angle mapping-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

% -------------------------------------------------------------------------
% a) plot ebsd data in a map with ipdf colorcoding wrt to z
% and a direction paralle to NW-SE  (z in our example is the kinematic/strain y-direction,
% and the flow plane is oriented NW-SE
% plot also a pole figure of c,a,-a,m,r,z where individual points are colorcoded
% with the ipdf color-coding
% hint: have a look at oM = ipdfHSVOrientationMapping and oM.inversePoleFigureDirection
hs= [... % planes
 Miller(0,0,0,1,cs,'hkl'),... %c
 Miller(1,1,-2,0,cs,'hkl'),... %a
 Miller(-1,-1,2,0,cs,'hkl'),... %-a
 Miller(1,0,-1,0,cs,'hkl'),... %m
 Miller(0,1,-1,1,cs,'hkl'),... %z
 Miller(1,0,-1,1,cs,'hkl')... %r
 ];


% -------------------------------------------------------------------------
% b) plot ebsd data color-coded with respect to a fibre e.g. with 0,0,0,1 
% parallel to the mean c-axis direction in a range of 20 degree
% hint get the mean direction of the c-axis from and use it as inversePoleFigureDirection
% in the orientation mapping
% hint: use oM = ipdfCenterOrientationMapping for as color-mapping and define
% a kernel for oM.psi



% -------------------------------------------------------------------------
% c) plot the direction of the c-axis in a map in a nice color coding
% hint: the c-axis is just a non-polar vector, use .oM.Miller2color
% how about TSLOrientationMapping(xvector)?
% hint: when plotting such an oM, you might need to specify the axis
% directions such as  plot(oMs,'zAxisDirection','intoPlane','xAxisDirection','east')


% -------------------------------------------------------------------------
% d) plot the angle between flow pane (NW-SW) and an a-axis and plot only those
% within a range < 30 degree
% hint: first we need to get all eq a-axes and we can loop through all
% angles between the a-axes and the reference direction and find the minimal
% value 
% some more hints:
% all_a = symmetrise(a) % gives all symetrical equivalents of a
% to define a loop, you can do something like the following:
% for i=1:lenth(all_a)
%    some_result(:,i)=angle(of something with all_a(i))
% end
% min(some_result,[],2)  gives the minimum in the second dimension, that is
% the minimum for each row
% 


