%% -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% 
%              /$$$$$$$$ /$$$$$$$   /$$$$$$  /$$$$$$$                       
%             | $$_____/| $$__  $$ /$$__  $$| $$__  $$                      
%             | $$      | $$  \ $$| $$  \__/| $$  \ $$                      
%             | $$$$$   | $$$$$$$ |  $$$$$$ | $$  | $$                      
%             | $$__/   | $$__  $$ \____  $$| $$  | $$                      
%             | $$      | $$  \ $$ /$$  \ $$| $$  | $$                      
%             | $$$$$$$$| $$$$$$$/|  $$$$$$/| $$$$$$$/                      
%             |________/|_______/  \______/ |_______/         

%                             /$$                                           
%                            | $$                                           
%                          /$$$$$$$$                                        
%                         |__  $$__/                                        
%                            | $$                                           
%                            |__/          

%         /$$$$$$  /$$$$$$$   /$$$$$$  /$$$$$$ /$$   /$$  /$$$$$$           
%        /$$__  $$| $$__  $$ /$$__  $$|_  $$_/| $$$ | $$ /$$__  $$          
%       | $$  \__/| $$  \ $$| $$  \ $$  | $$  | $$$$| $$| $$  \__/          
%       | $$ /$$$$| $$$$$$$/| $$$$$$$$  | $$  | $$ $$ $$|  $$$$$$           
%       | $$|_  $$| $$__  $$| $$__  $$  | $$  | $$  $$$$ \____  $$          
%       | $$  \ $$| $$  \ $$| $$  | $$  | $$  | $$\  $$$ /$$  \ $$          
%       |  $$$$$$/| $$  | $$| $$  | $$ /$$$$$$| $$ \  $$|  $$$$$$/          
%        \______/ |__/  |__/|__/  |__/|______/|__/  \__/ \______/       

%                                                    /$$                    
%                                                   |__/                    
%   /$$$$$$  /$$   /$$  /$$$$$$   /$$$$$$   /$$$$$$$ /$$  /$$$$$$$  /$$$$$$ 
%  /$$__  $$|  $$ /$$/ /$$__  $$ /$$__  $$ /$$_____/| $$ /$$_____/ /$$__  $$
% | $$$$$$$$ \  $$$$/ | $$$$$$$$| $$  \__/| $$      | $$|  $$$$$$ | $$$$$$$$
% | $$_____/  >$$  $$ | $$_____/| $$      | $$      | $$ \____  $$| $$_____/
% |  $$$$$$$ /$$/\  $$|  $$$$$$$| $$      |  $$$$$$$| $$ /$$$$$$$/|  $$$$$$$
%  \_______/|__/  \__/ \_______/|__/       \_______/|__/|_______/  \_______/
%                                                                           
%                                                                           
%                                                                           
%                                                                      
% The aim of this exercise is to become more familiar with ebsd data and
% grains, access and plot properties of grains

                                                       
%% 5) grains -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

% let's use cleaned data
% (cleanup procedure was: grains were calculated, non-indexed areas <= 2px have been filled, grains 
% calculated again and dauphine twins were merged, qtz-grains of 1 px have been deleted and filled
% been deleted and filled)
load ebsd_clean_workshop.mat

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


% make grains inspect grains and access grains properties
[grains,ebsd.grainId,ebsd.mis2mean] = calcGrains(ebsd,'angle',10*degree);
% grains are the grains
% grainId tells every ebsd pixel to which grain it belongs
% mis2mean tells each ebsd pixel it's misorientation to the mean grain orientation

% -------------------------------------------------------------------------
% a) explore the properties of grains:
% plot the frequency distribution of the grain orientation spread (.GOS) and
% colorcode each grains acording to the GOS



% -------------------------------------------------------------------------
% b) plot a histogram of the area equivalent diameter of grains, what is
% the frequency weighted mean/mode grain size? (for simplicty we skip the
% area/volume conversion)


% So far we did not consider twinning, so keep those numbers in mind
% and go on to see if we were wrong with our gs estimate

% -------------------------------------------------------------------------
% c) grains have boundaries: How long is the total qtz-qtz boundary in mm
% hint: inspect grains.boundary('q','q').[TAB]


% -------------------------------------------------------------------------
% d) plot the misorientation angle distribution
% hint: inspect grains.boundary('q','q').[TAB]
% and also consider using plotAngleDistribution
% option 1)


% option 2)


% in each case, we not that there is a huge peak at 60degree,
% this is due to dauphine twinning, a 60 degree rotation around the c-axis

% -------------------------------------------------------------------------
%%  e) get rid of dauphine twins
% hint: you can use merge, however it comes with a new meanorientation of
% grains which might not be any of the twinned portions, hence setting a
% higer symmetry might be an option

% option 1) merge the grains
% get the grain boundaries
gb_qtz = grains.boundary('q','q');
 %define a rotation assosciated with dauphine twinning
rot = rotation('axis',Miller(0,0,0,1,ebsd('q').CS),'angle',60*degree);
% calculates the angle between two trotations - the misorientation and the twinning rotation 
ang_r = angle(gb_qtz('q','q').misorientation,rot);
% condition/index all angles below a certain threshold
ind = ang_r < 6.*degree;
%select the boundaries based on this condition                                                                        
twinBoundary = gb_qtz(ind);
% merge grains connected with that twin boundary
[grains_merged,parentID_grains]= merge(grains,twinBoundary);
% make a copy of the ebsd dataset
ebsd_merged = ebsd;
% update the grainIds to the parentIds, so each ebsd_merged pixel knows
% again to which grain it belongs to
ebsd_merged.grainId  = parentID_grains(ebsd.grainId);
% plot it
figure
plot(ebsd('q'),ebsd('q').orientations,'faceAlpha',0.3)
hold on
plot(grains_merged('q').boundary,'linewidth',2)
hold on
plot(twinBoundary,'linecolor','r','linewidth',2)

% option 2) temporarily switch to higher symmetry

csq= ebsd('q').CS
cshq = crystalSymmetry('622','mineral', 'Quartz')
ebsd('q').CS=cshq
[grainsD,ebsd.grainId,ebsd.mis2mean] = calcGrains(ebsd,'angle',10*degree);
ebsd('q').CS=csq
grainsD('q').CS=csq
hold on
plot(grainsD.boundary('q','q'),'linecolor','b','linewidth',2)
hold off


%form here, go on with grains_merged / ebsd_merged or grainsD/ebsd
%%
% e) plot "real" equivalent diameter in a map and in a histogram
% compare to the previously calculated grains which were not merged, any
% difference?

% -------------------------------------------------------------------------
% f) plot a map of grains which have an aspect ratio larger 2
%create a new variable of qtz-only ebsd (makes some things easier)
gmqtz = grainsD('q');

%define a condition for grains and plot those where condition is true


% -------------------------------------------------------------------------
% g) plot only grains with an aspect ratio larger 2 AND with a long axis
% direction +/- 10 degree from NW-SE, plot ipdf standar colorcoding
%get long axis direction
[ev,ew]= gmqtz.principalComponents; %ev being the angle as complex, ew the length of axs
laxdir = atan2(ev(1,2,:),ev(1,1,:))./degree.*-1;
laxdir=laxdir(:);
%dir could go ccw with 0 in the east
laxdir(laxdir<0) = 180 + laxdir(laxdir<0);

% get the angle between vector3d(2,1,0) and laxdir within range of 10
% degree, in our case 2,1,0 is eqal to 153.4 (otherwise one can use polar, take care for the correct direction)
% and defrine a second conditon, you can use logical operators to combine
% those 


% -------------------------------------------------------------------------
% h) compare the area weighted pole figures of large and small grains and
% compare only the pole figures obtained by grains.meanOrientation



%% 6) misorientations-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

% have a look at grans.boundary.misorientations.[TAB]

% a) colorcode grain boundaries according to the misorientation angle on
% top of a semi-tranparent map showing grain meanorientation
% 'FaceAlpha' 0-1 should take case of transparency



% b) plot and colorcode the misorientation axes along boundaries with respect 
% to crystal ref. frame. Those will be of class Miller, so you can use an
% orinetation mapping with Miller2Color



% c) plot a map with only those boundaries which have misorientation axes 
% close to 10-10


% d) plot and colorcode the misorientation axes along boundaries with
% respect to sample ref. frame. Those will be of class vector3d
% get orientations along boundary
% hint: you can get the orientations along the boundary from
% ebsd(grains.boundary.ebsdID).orientations
% see also help orientation/axis


% plot the above misorientation axes in a pf/ipf with misorientation angles between 10 and
% 20degree 



% -------------------------------------------------------------------------
%i) Big-Exercise: Compare misorientations of low angle boundaries (2-10 degree)
% of two types of grains, those with a flat and those with a steep c-axis orientation,
% plot misorientation axes in sample and in crystal reference frame, plot maps of 
% grain boundaries, with a colorcoding for misorientation axes in crystal and sample reference frame
% Note: if you want to access boundaries with smaller misorientation angles, 
% you have to segment grains with a smaller threshold and select relevant angles above noise
% ebsd_s= ebsd
% [subgrains,ebsd_S.grainId,ebsd_s.mis2mean] = calcGrains(ebsd,'angle',0.5*degree);

% 0) get the c-axis direction

% 1) define grains with the c-axis close to the center
% you can use the angle between the c-axis nad zvector

% 2) define with the c-axis close to the periphery

% 3) get the ebsd of selected  grains
% e.g ebsd1 = ebsd(grains(cond1));
% e.g ebsd2 = ebsd(grains(cond2));

% 4) make new grains with a very small threshold


% 5) get the (sub)grain boundaries


% 6) get the misorientation angle

% 7) get the misorientation axis (crytal ref. frame)

% 8) get misor axes (sample ref. frame)
% 8a)get the boundary orientations
% 8b)calc the axes

% 9) limit the range of angles to 2-10 degree


% 10)  make two pleasant colorcodings for misor axes

% 11) plot boundaries and and pole figures/ inverse pole figure of misor
% axes
