%% Script for generating primary mineral maps for EFS1 using a pre-trained neural network

% Published work using EFS1 should cite:
% Peters, C.A., and Kim, J.J. (2020). "Eagle Ford Shale: Synchrotron-Based Element and Mineral Maps". 
% Digital Rocks Portal. [Online]. Available: http://www.digitalrocksportal.org. DOI: 

%% Related Publication for SMART: Synchrotron based Mapping Approach for RasTer Mineral Mapping

% Developers: Julie J. Kim, Florence T. Ling, Dan A. Plattenberger, 
% Andres F. Clarens, Antonio Lanzirotti, Matthew Newville, 
% Stephen R. Sutton, Catherine A. Peters

% Environmental Geochemistry Lab, http://www.princeton.edu/~cap
% Department of Civil & Environmental Engineering, Princeton University

% Contact: Prof. Catherine A. Peters, Send email to cap@princeton.edu

% Published work using SMART should cite:
% Kim, J.J., et al. (year). DOI:

%% Initialization
clc;
clear all;
close all;

% Required input (2): EFS1_coupled_inputdata.mat & EFS_trained_network.mat
% Generated output (1): 1 figure, 3 mineral maps 

%% EFS1: Results of SMART MINERAL MAPPER

% Read saved XRF intensity data for EFS1:
% Input rows 1-2 contain pixel coordinates and 
% rows 3-10 contain normalized intensities from 8 selected elements. 
% Normalizations were based on Kim et al. (Year) 

load Data_EFS1_input    %replace with correct file name for input data

% Load trained network

load Data_EFS1_trained_network  %replace with correct file name for trained network 

% Apply SMART mineral mapper
real_test = net_new_EFS_v7(compiled_EFS_short([3:10],:));      %replace compiled_EFS_short with correct imported matrix name

%% Plotting

% set indices for plotting
plotindex = [4,23,11,12,21,25,10,9,1,18,15,16,2,3,13,5,6,8,22,24,7,17,20,14,19,26]; %indices for plotting by mineral groups
names = ["Calcite","Aragonite","Magnesite","Siderite","Dolomite","Pyrite","Mackinawite","Pyrrhotite","Chalcopyrite","Arsenopyrite","Ilmenite","Welinite","Anatase","Goethite","Pyrolusite","Cristobalite","Albite","Almandine","Tridymite","Augite","Anorthite","Analcime","Kaolinite","Chlorite","Anhydrite"];

% 3 mineral plots - subplots of calcite, pyrite, quartz
figure;
hFig=gcf;
set(0,'DefaultAxesTitleFontWeight','normal');
pointsize = 10;
mkr = 's';
k=1;

% Pixel coordinates
x_val = compiled_EFS_short(1,:);    %replace with correct variable name
y_val = compiled_EFS_short(2,:);    %replace with correct variable name

% Calcite
for i = 1   % put in the mineral index number
    subplot(1,3,k)
    scatter(x_val,y_val,pointsize,real_test(plotindex(i),:),mkr,'filled') 
    hcb4=colorbar;
    colormap(gca,'bone')
    caxis([0 1])   
    axis equal
end
title("Calcite",'FontSize',14)
axis([0 250 0 250])         %updae axis
set(gca,'Yticklabel',[]) 
set(gca,'Xticklabel',[])
hold on

% Pyrite
mkr = 's';
k=2;
for i = 6   % put in the mineral index number
    subplot(1,3,k)
    scatter(x_val,y_val,pointsize,real_test(plotindex(i),:),mkr,'filled') 
    hcb4=colorbar;
    colormap(gca,'pink')
    caxis([0 1])   
    axis equal
end
title("Pyrite",'FontSize',14)
axis([0 250 0 250])     %updae axis
set(gca,'Yticklabel',[]) 
set(gca,'Xticklabel',[])
hold on

% Quartz
mkr = 's';
k=3;
for i = 26  % put in the mineral index number
    subplot(1,3,k)
    scatter(x_val,y_val,pointsize,real_test(plotindex(i),:),mkr,'filled') 
   
    hcb4=colorbar;
    colormap(gca,'copper')
    ylabel(hcb4,'Likelihood','FontSize',10)
    caxis([0 1])   
    axis equal
end
title("Quartz",'FontSize',14)
axis([0 250 0 250])      %updae axis
set(gca,'Yticklabel',[]) 
set(gca,'Xticklabel',[])

hold off


