%% Import the Data
% create an EBSD variable containing the data
ebsd = loadEBSD(fname,CS,'interface','crc',...
'convertSpatial2EulerReferenceFrame');
% % Take a look at the initial data without the non-indexed points
% plot(ebsd('indexed'))
% ######### Correction #########
% plot(ebsd,ebsd.mad)
% mtexColorbar
% % plot a MAD histogram
% hist(ebsd.mad)
% take only those measurements with MAD smaller than prescribed value set them to nonIndexed
ebsd(ebsd.mad>1.2).phase=0;
% ######### Adjust (rotate & crop) #########
% rotate the whole data set (orientations and spatial) about the z-axis by
% 90*degree to get vertical strut
ebsd = rotate(ebsd,-90*degree);
% % plot the band contrast
% plot(ebsd,ebsd.bc)
% colormap gray % this makes the image grayscale
% mtexColorbar
% segmentation angle typically 10 to 15 degrees
seg_angle = 15;
% minimum indexed points per grain between 5 and 10
min_points = 0;
% compute grains. either use 'angle' or 'unitcell' for segmentation
[grains,ebsd.grainId] = calcGrains(ebsd('indexed'),'unitcell');
% [grains,ebsd.grainId,ebsd.mis2mean] = calcGrains(ebsd,'angle',seg_angle*degree);
% ######### Clean #########
% Remove small grains with less than min_points indexed points
grains = grains(grains.grainSize > min_points);
% Re-calculate grain model to cleanup grain boundaries with less than minimum index points
% used ebsd points within grains having the minimum indexed number of points (e.g. 5~10 points)
ebsd = ebsd(grains);
[grains,ebsd.grainId] = calcGrains(ebsd('indexed'),'unitcell');
% [grains,ebsd.grainId,ebsd.mis2mean] = calcGrains(ebsd,'angle',seg_angle*degree,'boundary','tight');
% ######### Smooth #########
% Due to the measurement grid, the grain boundaries often show a typical staircase effect.
% This effect can be reduced by smoothing the grain boundaries.
grains = smooth(grains,4);
ebsd = ebsd(grains);