Files

406 lines
22 KiB
Matlab

function DataTable = nappNorm(DataTable, processSelection, refSelection, options)
% NAPPNORM Normalize XPS spectra based on provided reference selection or self-maximum.
%
% DataTable = NAPPNORM(DataTable, processSelection, refSelection) normalizes
% the spectra where processSelection is true, dividing them by the peak area
% of matched spectra from the pool where refSelection is true. Both must
% be logical arrays matching the height of the DataTable.
%
% DataTable = NAPPNORM(DataTable, processSelection) scales each spectrum
% selected by the logical array by its own maximum value.
%
% Name-Value arguments:
% matchStrategy - 'earlier', 'after', 'spectrumNumber', 'internal'
% tableVariable - Target column ('spectrum_countsPerSecond' by default)
% timeVariable - Target time column ('lastModifiedDatenum' by default)
% peakNumber - Which peak area to use if multiple exist (1 by default)
% mode - "max" per default, "area" as option
% range- energy range where max is found. first mist be first value, second
% second (low to high for kinetic, high to low for binding)
arguments
DataTable table
processSelection logical {mustBeVector}
% refSelection is optional. If omitted, it defaults to empty.
refSelection logical {mustBeVector} = false(0,1);
% Name-Value arguments
options.matchStrategy char {mustBeMember(options.matchStrategy, ...
{'earlier', 'after', 'spectrumNumber', 'internal'})} = 'earlier'
options.tableVariable char {mustBeMember(options.tableVariable, ...
{'spectrum_counts', 'spectrum_countsPerSecond', 'spectrum_countsNorm'})} = 'spectrum_countsPerSecond'
options.timeVariable char {mustBeMember(options.timeVariable, ...
{'lastModifiedDatenum', 'creationDate'})} = 'lastModifiedDatenum'
options.peakNumber (1,1) double {mustBeInteger, mustBePositive} = 1
options.mode char {mustBeMember(options.mode, ...
{'max', 'area'})} = 'max'
options.range (1,:) double {mustBePositive} =[ ];
options.energyScale char {mustBeMember(options.energyScale, ...
{'kinetic', 'binding'})} = 'kinetic'
options.spectrumNumberPrefixLength double {mustBePositive, mustBeInteger} = 7
end
numRowsTable = height(DataTable);
% Validate length of the logical arrays
if length(processSelection) ~= numRowsTable
error('processSelection must have the exact same length as the DataTable height (%d).', numRowsTable);
end
if ~isempty(refSelection) && any(refSelection) && length(refSelection) ~= numRowsTable
error('refSelection must have the exact same length as the DataTable height (%d).', numRowsTable);
end
% Auto-detect mode: If no valid reference array is passed OR 'internal' is chosen
if strcmpi(options.matchStrategy, 'internal') && ( isempty(refSelection) || ~any(refSelection) ) && strcmpi(options.mode, 'max')
modus = 'internalMax';
elseif strcmpi(options.mode, 'max')
modus = 'externalMax';
else
modus = 'referenceArea';
end
% Initialize new columns if they don't exist
if ~ismember('spectrum_countsNorm', DataTable.Properties.VariableNames)
DataTable.spectrum_countsNorm = cell(numRowsTable, 1);
end
if ~ismember('lognappNormMode', DataTable.Properties.VariableNames)
DataTable.lognappNormMode = cell(numRowsTable, 1);
end
if ~ismember('lognappNormWith', DataTable.Properties.VariableNames)
DataTable.lognappNormWith = cell(numRowsTable, 1); % New column for spectrum name
end
%% Processing Loop
processIdx = find(processSelection);
refIdx = find(refSelection);
for i = 1:length(processIdx)
currRow = processIdx(i);
yData = DataTable.(options.tableVariable){currRow};
currTime = datenum(DataTable.(options.timeVariable)(currRow, :));
refTimes = datenum(DataTable.(options.timeVariable)(refIdx, :));
currName = DataTable.name{currRow};
bestRefRow = NaN;
if isempty(yData)
continue;
end
switch modus
case 'internalMax'
if ~isempty(options.range)
% Auto-detect available energy column for this row
if strcmpi(options.energyScale, 'kinetic') && ...
ismember('scientaEkin_eV', DataTable.Properties.VariableNames) && ...
~isempty(DataTable.scientaEkin_eV{currRow})
xData = DataTable.scientaEkin_eV{currRow};
elseif ismember('bindingEnergy_eV', DataTable.Properties.VariableNames) && ...
~isempty(DataTable.bindingEnergy_eV{currRow})
xData = DataTable.bindingEnergy_eV{currRow};
else
warning('Row %d (%s): No valid energy column found. Falling back to full-spectrum max.', ...
currRow, DataTable.name{currRow});
xData = [];
end
if ~isempty(xData)
% min/max of range handles both ascending and descending order
minR = min(options.range);
maxR = max(options.range);
idxRange = (xData >= minR) & (xData <= maxR);
if ~any(idxRange)
warning('Row %d (%s): options.range [%.4g %.4g] contains no data points (xData spans [%.4g %.4g]). Falling back to full-spectrum max.', ...
currRow, DataTable.name{currRow}, minR, maxR, min(xData), max(xData));
normFactor = max(yData);
else
searchY = yData(idxRange);
[normFactor, relativeMaxIdx] = max(searchY);
validIndices = find(idxRange);
absoluteMaxIndex = validIndices(relativeMaxIdx);
fprintf('Norming %s at %.1f eV: %.1e\n', DataTable.name{currRow}, xData(absoluteMaxIndex), normFactor);
end
else
normFactor = max(yData);
end
else
normFactor = max(yData);
end
if normFactor == 0, normFactor = 1; end % Protect against /0
DataTable.spectrum_countsNorm{currRow} = yData ./ normFactor;
DataTable.lognappNormMode{currRow} = 'maximum';
DataTable.lognappNormWith{currRow} = num2str(normFactor);
case 'referenceArea'
% Internal Matching Engine using Code Words
switch options.matchStrategy
case 'earlier'
validRefs = refIdx(refTimes <= currTime);
if ~isempty(validRefs)
timeDiffs = currTime - refTimes(refTimes <= currTime);
[~, minIdx] = min(timeDiffs);
bestRefRow = validRefs(minIdx);
end
case 'after'
validRefs = refIdx(refTimes >= currTime);
if ~isempty(validRefs)
timeDiffs = refTimes(refTimes >= currTime) - currTime;
[~, minIdx] = min(timeDiffs);
bestRefRow = validRefs(minIdx);
end
case 'spectrumNumber'
% Use the first N characters of the name as the matching prefix
prefixLen = options.spectrumNumberPrefixLength;
if length(currName) >= prefixLen
searchPattern = currName(1:prefixLen);
validRefs = refIdx(startsWith(DataTable.name(refIdx), searchPattern));
if ~isempty(validRefs)
bestRefRow = validRefs(1);
end
else
warning('Row %d: Name "%s" is shorter than the requested prefix length (%d). Skipping.', ...
currRow, currName, prefixLen);
end
% Validate we found a match
if isnan(bestRefRow)
warning('Row %d (%s): No matching reference found for strategy "%s". Skipping.', ...
currRow, currName, options.matchStrategy);
continue;
end
% Extract area and validate
refAreas = DataTable.peakArea(bestRefRow,:);
if size(refAreas,2) < options.peakNumber
warning('Row %d: peakNumber %d exceeds available peaks in reference row %d. Skipping.', ...
currRow, options.peakNumber, bestRefRow);
continue;
end
normFactor = refAreas(options.peakNumber);
if normFactor == 0
warning('Row %d: Reference area in row %d is zero. Skipping.', currRow, bestRefRow);
continue;
end
% Apply normalization and log the exact name of the reference
DataTable.spectrum_countsNorm{currRow} = yData ./ normFactor;
DataTable.lognappNormMode{currRow} = sprintf('referenceArea (Row %d)', bestRefRow);
DataTable.lognappNormWith{currRow} = DataTable.name{bestRefRow};
% -------------------------------------------------------------------------
% 1. Dynamically get this function's name
thisFuncName = mfilename;
colDate = sprintf('log%sDate', thisFuncName);
colCaller = sprintf('log%sCaller', thisFuncName);
% 2. Probe for column existence, create empty string arrays if missing
if ~ismember(colDate, DataTable.Properties.VariableNames)
DataTable.(colDate) = repmat("", numRowsTable, 1);
end
if ~ismember(colCaller, DataTable.Properties.VariableNames)
DataTable.(colCaller) = repmat("", numRowsTable, 1);
end
% 3. Get current time as a string
currentTimeStr = string(datetime('now', 'Format', 'yyyy-MM-dd HH:mm:ss'));
% 4. Determine the calling script using the call stack
stackInfo = dbstack;
if length(stackInfo) > 1
callerScript = string(stackInfo(2).file); % The script that called this function
else
callerScript = " "; % Called directly from the command window
end
% 5. Write the log data only to the rows being processed
DataTable.(colDate)(processIdx) = currentTimeStr;
DataTable.(colCaller)(processIdx) = callerScript;
end % -------------------------------------------------------------------------
case 'externalMax'
% Internal Matching Engine using Code Words
switch options.matchStrategy
case 'earlier'
validRefs = refIdx(refTimes <= currTime);
if ~isempty(validRefs)
timeDiffs = currTime - refTimes(refTimes <= currTime);
[~, minIdx] = min(timeDiffs);
bestRefRow = validRefs(minIdx);
if ~isempty(options.range)
if strcmpi(options.energyScale, 'kinetic')
% Extract the X and Y data for cleaner code
xData = DataTable.scientaEkin_eV{bestRefRow};
yDataRef = DataTable.(options.tableVariable){bestRefRow};
% Find all points within the range (safely handles ascending/descending)
minR = min(options.range);
maxR = max(options.range);
idxRange = (xData >= minR) & (xData <= maxR);
% Extract the subset and find the max
searchY = yDataRef(idxRange);
[normFactor, relativeMaxIdx] = max(searchY);
% Convert the relative index back to the absolute index to get the true energy
validIndices = find(idxRange);
absoluteMaxIndex = validIndices(relativeMaxIdx);
fprintf('Norming %s with %s at %.1f eV: %.1e\n', DataTable.name{currRow}, DataTable.name{bestRefRow}, xData(absoluteMaxIndex),normFactor);
else
% Extract the X and Y data for cleaner code
xData = DataTable.bindingEnergy_eV{bestRefRow};
yDataRef = DataTable.(options.tableVariable){bestRefRow};
% Find all points within the range (safely handles ascending/descending)
minR = min(options.range);
maxR = max(options.range);
idxRange = (xData >= minR) & (xData <= maxR);
% Extract the subset and find the max
searchY = yDataRef(idxRange);
[normFactor, relativeMaxIdx] = max(searchY);
% Convert the relative index back to the absolute index to get the true energy
validIndices = find(idxRange);
absoluteMaxIndex = validIndices(relativeMaxIdx);
fprintf('Norming %s with %s at %.1f eV: %.1e\n', DataTable.name{currRow}, DataTable.name{bestRefRow}, xData(absoluteMaxIndex),normFactor);
end
else
[normFactor, MaxIdx] = max(DataTable.(options.tableVariable){bestRefRow});
if strcmpi(options.energyScale, 'kinetic')
fprintf('Norming %s with %s at %.1f eV: %.1e\n', DataTable.name{currRow}, DataTable.name{bestRefRow}, DataTable.scientaEkin_eV{bestRefRow}(MaxIdx),normFactor );
else
fprintf('Norming %s with %s at %.1f eV: %.1e\n', DataTable.name{currRow}, DataTable.name{bestRefRow}, DataTable.bindingEnergy_eV{bestRefRow}(MaxIdx),normFactor );
end
end
if normFactor == 0, normFactor = 1; end % Protect against /0
DataTable.spectrum_countsNorm{currRow} = yData ./ normFactor;
DataTable.lognappNormMode{currRow} = 'external maximum';
DataTable.lognappNormWith{currRow} = num2str(normFactor); % Log internal maximum
end
case 'after'
validRefs = refIdx(refTimes >= currTime);
if ~isempty(validRefs)
timeDiffs = refTimes(refTimes >= currTime) - currTime;
[~, minIdx] = min(timeDiffs);
bestRefRow = validRefs(minIdx);
if ~isempty(options.range)
if strcmpi(options.energyScale, 'kinetic')
% Extract the X and Y data for cleaner code
xData = DataTable.scientaEkin_eV{bestRefRow};
yDataRef = DataTable.(options.tableVariable){bestRefRow};
% Find all points within the range (safely handles ascending/descending)
minR = min(options.range);
maxR = max(options.range);
idxRange = (xData >= minR) & (xData <= maxR);
% Extract the subset and find the max
searchY = yDataRef(idxRange);
[normFactor, relativeMaxIdx] = max(searchY);
% Convert the relative index back to the absolute index to get the true energy
validIndices = find(idxRange);
absoluteMaxIndex = validIndices(relativeMaxIdx);
fprintf('Norming %s with %s at %.1f eV: %.1e\n', DataTable.name{currRow}, DataTable.name{bestRefRow}, xData(absoluteMaxIndex),normFactor);
else
% Extract the X and Y data for cleaner code
xData = DataTable.bindingEnergy_eV{bestRefRow};
yDataRef = DataTable.(options.tableVariable){bestRefRow};
% Find all points within the range (safely handles ascending/descending)
minR = min(options.range);
maxR = max(options.range);
idxRange = (xData >= minR) & (xData <= maxR);
% Extract the subset and find the max
searchY = yDataRef(idxRange);
[normFactor, relativeMaxIdx] = max(searchY);
% Convert the relative index back to the absolute index to get the true energy
validIndices = find(idxRange);
absoluteMaxIndex = validIndices(relativeMaxIdx);
fprintf('Norming %s with %s at %.1f eV: %.1e\n', DataTable.name{currRow}, DataTable.name{bestRefRow}, xData(absoluteMaxIndex),normFactor);
end
else
[normFactor, MaxIdx] = max(DataTable.(options.tableVariable){bestRefRow});
if strcmpi(options.energyScale, 'kinetic')
fprintf('Norming %s with %s at %.1f eV: %.1e\n', DataTable.name{currRow}, DataTable.name{bestRefRow}, DataTable.scientaEkin_eV{bestRefRow}(MaxIdx),normFactor );
else
fprintf('Norming %s with %s at %.1f eV: %.1e\n', DataTable.name{currRow}, DataTable.name{bestRefRow}, DataTable.bindingEnergy_eV{bestRefRow}(MaxIdx),normFactor );
end
end
if normFactor == 0, normFactor = 1; end % Protect against /0
DataTable.spectrum_countsNorm{currRow} = yData ./ normFactor;
DataTable.lognappNormMode{currRow} = 'external maximum';
DataTable.lognappNormWith{currRow} = num2str(normFactor); % Log internal maximum
end
case 'spectrumNumber'
% Extract the 4-digit number at the start of the name (e.g., '0033')
specNumStr = regexp(currName, '^\d{4}', 'match', 'once');
if ~isempty(specNumStr)
% Find references that start with 'XXXX_'
searchPattern = [specNumStr, '_'];
validRefs = refIdx(startsWith(DataTable.name(refIdx), searchPattern));
if ~isempty(validRefs)
bestRefRow = validRefs(1); % Take the first exact match
end
else
warning('Row %d: Could not extract a 4-digit spectrum number from name "%s".', currRow, currName);
end
if ~isempty(options.range)
if strcmpi(options.energyScale, 'kinetic')
% Extract the X and Y data for cleaner code
xData = DataTable.scientaEkin_eV{bestRefRow};
yDataRef = DataTable.(options.tableVariable){bestRefRow};
% Find all points within the range (safely handles ascending/descending)
minR = min(options.range);
maxR = max(options.range);
idxRange = (xData >= minR) & (xData <= maxR);
% Extract the subset and find the max
searchY = yDataRef(idxRange);
[normFactor, relativeMaxIdx] = max(searchY);
% Convert the relative index back to the absolute index to get the true energy
validIndices = find(idxRange);
absoluteMaxIndex = validIndices(relativeMaxIdx);
fprintf('Norming %s with %s at %.1f eV: %.1e\n', DataTable.name{currRow}, DataTable.name{bestRefRow}, xData(absoluteMaxIndex),normFactor);
else
% Extract the X and Y data for cleaner code
xData = DataTable.bindingEnergy_eV{bestRefRow};
yDataRef = DataTable.(options.tableVariable){bestRefRow};
% Find all points within the range (safely handles ascending/descending)
minR = min(options.range);
maxR = max(options.range);
idxRange = (xData >= minR) & (xData <= maxR);
% Extract the subset and find the max
searchY = yDataRef(idxRange);
[normFactor, relativeMaxIdx] = max(searchY);
% Convert the relative index back to the absolute index to get the true energy
validIndices = find(idxRange);
absoluteMaxIndex = validIndices(relativeMaxIdx);
fprintf('Norming %s with %s at %.1f eV: %.1e\n', DataTable.name{currRow}, DataTable.name{bestRefRow}, xData(absoluteMaxIndex),normFactor);
end
else
[normFactor, MaxIdx] = max(DataTable.(options.tableVariable){bestRefRow});
if strcmpi(options.energyScale, 'kinetic')
fprintf('Norming %s with %s at %.1f eV: %.1e\n', DataTable.name{currRow}, DataTable.name{bestRefRow}, DataTable.scientaEkin_eV{bestRefRow}(MaxIdx),normFactor );
else
fprintf('Norming %s with %s at %.1f eV: %.1e\n', DataTable.name{currRow}, DataTable.name{bestRefRow}, DataTable.bindingEnergy_eV{bestRefRow}(MaxIdx),normFactor );
end
end
if normFactor == 0, normFactor = 1; end % Protect against /0
DataTable.spectrum_countsNorm{currRow} = yData ./ normFactor;
DataTable.lognappNormMode{currRow} = 'external maximum';
DataTable.lognappNormWith{currRow} = num2str(normFactor); % Log internal maximum
end
end
end