function [ field, pol ] = NaFeCuAs_ZF_MuSR_Sim( muonLoc, impCon, numMC )
%NAFECUAS_ZF_SIM Summary of this function goes here
%   Detailed explanation goes here

%Input Parsing
p = inputParser;

%Parsing conditions
addRequired(p, 'muonLoc', @(x) validateattributes( x, ...
    {'numeric'}, {'nonempty','vector','numel',3} ) );
addRequired(p, 'impCon', @(x) validateattributes( x, ...
    {'numeric'}, {'nonempty','scalar','nonnegative','<=',1} ) );
addRequired(p, 'numMC', @(x) validateattributes( x, ...
    {'numeric'}, {'nonempty','scalar','positive','integer'} ) );

%Parsing Input
parse(p, muonLoc, impCon,numMC);

%Assigning inputs
muonLoc = p.Results.muonLoc;
impCon = p.Results.impCon;
numMC = p.Results.numMC;

%Delete parser
clear p

%Get Dipolar Fields
%Settings
numCell = 5;
%Define Electronic moments
spinUp = [1,1,0]/sqrt(2);
spinDown = -[1,1,0]/sqrt(2);
%Spins
spinFe = [spinUp;spinUp];
%spinFe = zeros(2,3);

%make lattice
fprintf('Making Lattice.\n');
tic;
[magLattice, nucLattice] = Make_NaFeAs_Lattice(numCell, spinFe);
toc;
fprintf('Computing Dipolar Field.\n');

tic;
field = Compute_Field_NaFeCuAs(muonLoc, impCon, numMC, ...
    magLattice{1}, magLattice{2}, ...
    nucLattice{1}, nucLattice{2} );
toc;

%Compute spin polarziation
fprintf('Computing Polarization.\n');
times = linspace(0,1e-5,1e3);

%For loop over NSR/SR
for k = 1:3
    %choose NSR/SR
    textSR = {'SRz','SRx','SRy'};
    switch k
        case 1 %NSR (z)
            S0 = [0,0,1];
        case 2 %SR (x)
            S0 = [1,0,0];
        case 3 %SR (y)
            S0 = [0,1,0];
%         case 2 %SR (random x,y)
%             normXY = normr(rand(numMC,2)-1/2);
%             S0 = [normXY,zeros(numMC,1)];
    end
    tic;
    pol = Compute_Spin_Polarization(field,times,S0);
    toc;

    f=figure(1);
    hold on;
    colors = Get_Colors(3,'qual');
    for j = 1:3
        p(j) = plot(times,pol(:,j),'Color',colors(j,:),'LineWidth',2);
    end
    legend(p,{'P_x','P_y','P_z'});
    xlabel('Time (s)');
    ylabel('Normalized Polarization');
    
    title(sprintf('Muon Site: (%.3f,%.3f,%.3f), ZF-%s, x = %.2f', ...
        muonLoc(1),muonLoc(2),muonLoc(3), textSR{k}, impCon) ); 
    xlim([0,1e-5]);

    %Saving
%     saveFolder = ['D:\Documents\MuSR_Research\M1570_NaFe1-xCuxAs\'...
%         'Dipolar_Field_Simulation\ZF_Polarization_NaFeCuAs\Results_160615'];
    saveFolder = 'Plots';
    folderName = sprintf( ...
        'MuonPos_(%.3f,%.3f,%.3f)_ZF-%s', ...
                muonLoc(1),muonLoc(2),muonLoc(3), textSR{k} ...
                );
    saveFolderPath = [saveFolder,'\',folderName];
    Folder_Check(saveFolderPath);
%     mkdir(saveFolderPath);

    saveName = [saveFolderPath,...
        '\', folderName, '_x_', num2str(impCon,'%.2f')];
    print(f,'-dpng','-r600',...
        [saveName, '.png']);
    close(f);
end

end  %End of NaFeCuAs_ZF_MuSR_Sim.m

