136 lines
5.8 KiB
Matlab
136 lines
5.8 KiB
Matlab
function ListFiles = QuickAndDirtyNexafsImport(DataTable)
|
|
|
|
|
|
|
|
%% QuickAndDirtyNexafsImport
|
|
%
|
|
[filePick,pathPick] = uigetfile({'*.h5'},...
|
|
'Select One or More Files',...
|
|
'MultiSelect', 'on');
|
|
% make lists for Nexafs
|
|
if ischar(filePick)
|
|
filePick = {filePick};
|
|
end
|
|
|
|
for ii = 1:size(filePick,2)
|
|
TmpFileList{2}(ii,1) = dir([pathPick filePick{ii}]);
|
|
end
|
|
|
|
|
|
for ii = 1:size(filePick,2)
|
|
try
|
|
TmpImage= h5read([TmpFileList{2}(ii).folder filesep TmpFileList{2}(ii).name],'/scan 1/spectrum');
|
|
%else
|
|
catch
|
|
try
|
|
TmpImage = h5read([TmpFileList{2}(ii).folder filesep TmpFileList{2}(ii).name],'/scan 1/scienta spectrum');
|
|
catch
|
|
TmpData = h5read([TmpFileList{2}(ii).folder filesep TmpFileList{2}(ii).name],'/entry/instrument/detector/data');
|
|
%convert 3 dim matrix to 2 dim matrix
|
|
for i = 1:size(TmpData,3)%iterations
|
|
TmpImage(:,i) = TmpData(:,1,(i)); %#ok<*SAGROW>
|
|
end
|
|
|
|
|
|
|
|
end
|
|
end
|
|
% import Photon Energy (cols)
|
|
try %general try to ensure that script does not stop upon errors.
|
|
try
|
|
TmpXrayEkin = h5read([TmpFileList{2}(ii).folder filesep TmpFileList{2}(ii).name],'/scan 1/energy'); % X-ray excitation energy; Photon energy in eV
|
|
TmpXrayEkin(TmpXrayEkin==0)=[];
|
|
catch
|
|
TmpXrayEkin = h5read([TmpFileList{2}(ii).folder filesep TmpFileList{2}(ii).name],'/entry/instrument/NDAttributes/PhotonEnergy'); % X-ray excitation energy; Photon energy in eV
|
|
TmpXrayEkin(TmpXrayEkin==0)=[];
|
|
end
|
|
% Make unique
|
|
[TmpFileList{2}(ii).xRayEkin_eV, ind] = unique(TmpXrayEkin);
|
|
TmpFileList{2}(ii).image = TmpImage(:,ind);
|
|
% NEXAFS pass energy
|
|
try
|
|
tmp = h5read([TmpFileList{2}(ii).folder filesep TmpFileList{2}(ii).name],'/scan 1/attrs/PassEnergy'); % in eV
|
|
TmpFileList{2}(ii).scientaPassEnergy_eV = str2double(tmp(1));
|
|
catch
|
|
tmp = h5read([TmpFileList{2}(ii).folder filesep TmpFileList{2}(ii).name],'/entry/instrument/NDAttributes/PassEnergy'); % in eV
|
|
TmpFileList{2}(ii).scientaPassEnergy_eV = str2double(tmp(1));
|
|
end
|
|
% Acquisition mode
|
|
try
|
|
TmpFileList{2}(ii).scientaAcquisitionMode = h5read([TmpFileList{2}(ii).folder filesep TmpFileList{2}(ii).name],'/scan 1/attrs/AcquisitionMode');
|
|
catch
|
|
TmpFileList{2}(ii).scientaAcquisitionMode = h5read([TmpFileList{2}(ii).folder filesep TmpFileList{2}(ii).name],'/entry/instrument/NDAttributes/AcquireMode');
|
|
end
|
|
if strcmp(TmpFileList{2}(ii).scientaAcquisitionMode,'Fixed')
|
|
% NEXAFS kinetic energy window - fixed mode
|
|
try
|
|
tmp = h5read([TmpFileList{2}(ii).folder filesep TmpFileList{2}(ii).name],'/scan 1/attrs/ScientaCenterEnergy'); % in eV
|
|
catch
|
|
tmp = h5read([TmpFileList{2}(ii).folder filesep TmpFileList{2}(ii).name],'/entry/instrument/NDAttributes/CenterEnergy'); % in eV
|
|
end
|
|
TmpFileList{2}(ii).scientaEkinRange_eV = [tmp(1)-TmpFileList{2}(ii).scientaPassEnergy_eV/20 tmp(1)+TmpFileList{2}(ii).scientaPassEnergy_eV/20];
|
|
TmpFileList{2}(ii).scientaEkinStep_eV = NaN;
|
|
TmpFileList{2}(ii).scientaEkin_eV = linspace(TmpFileList{2}(ii).scientaEkinRange_eV(:,1)...
|
|
,TmpFileList{2}(ii).scientaEkinRange_eV(:,2),size(TmpFileList{2}(ii).image,1))';
|
|
else
|
|
% NEXAFS kinetic energy window - scan mode
|
|
try
|
|
tmp = h5read([TmpFileList{2}(ii).folder filesep TmpFileList{2}(ii).name],'/scan 1/attrs/ScientaLowEnergy'); % in eV
|
|
tmp2 = h5read([TmpFileList{2}(ii).folder filesep TmpFileList{2}(ii).name],'/scan 1/attrs/ScientaHighEnergy');
|
|
catch
|
|
tmp = h5read([TmpFileList{2}(ii).folder filesep TmpFileList{2}(ii).name],'/entry/instrument/NDAttributes/LowEnergy'); % in eV
|
|
tmp2 = h5read([TmpFileList{2}(ii).folder filesep TmpFileList{2}(ii).name],'/entry/instrument/NDAttributes/HighEnergy');
|
|
end
|
|
TmpFileList{2}(ii).scientaEkinRange_eV = [tmp(1) tmp2(1)];
|
|
try
|
|
TmpFileList{2}(ii).scientaEkinStep_eV = h5read([TmpFileList{2}(ii).folder filesep TmpFileList{2}(ii).name],'/scan 1/attrs/StepSize');
|
|
catch
|
|
TmpFileList{2}(ii).scientaEkinStep_eV = h5read([TmpFileList{2}(ii).folder filesep TmpFileList{2}(ii).name],'/entry/instrument/NDAttributes/StepSize');
|
|
end
|
|
TmpFileList{2}(ii).scientaEkin_eV = [TmpFileList{2}(ii).scientaEkinRange_eV(:,1):TmpFileList{2}(ii).scientaEkinStep_eV(1):TmpFileList{2}(ii).scientaEkinRange_eV(:,2)]';
|
|
end
|
|
catch
|
|
end
|
|
end
|
|
%% Merge lists into one
|
|
%
|
|
|
|
% get index of non-empty structures
|
|
tf =~cellfun(@isempty,TmpFileList);
|
|
|
|
% merge the non-empty list structures into one structure
|
|
ListFiles = cat(1,TmpFileList{tf});
|
|
|
|
% remove lines with empty images
|
|
for ii = 1:size(ListFiles,1)
|
|
tmpLogic(ii) = isempty(ListFiles(ii).image);
|
|
noncells = tmpLogic;
|
|
end
|
|
ListFiles(noncells) = [];
|
|
|
|
clearvars -except ListFiles DataTable
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
%% Sort by date
|
|
% rename date fields and delete those fields not needed
|
|
[ListFiles.lastModifiedDatestr] = ListFiles.date;
|
|
[ListFiles.lastModifiedDatenum] = ListFiles.datenum;
|
|
|
|
% First, we need to convert into a table, because structures can not be sorted:
|
|
ListFiles = struct2table(ListFiles,'AsArray',true);
|
|
% Then, we sort the table based on the time (6th column, datenum)
|
|
ListFiles = sortrows(ListFiles,6);
|
|
% Then, remove un-needed columns
|
|
ListFiles = removevars(ListFiles,{'bytes','isdir','date','datenum'});
|
|
|
|
% merge tables (append imported data to previous table)
|
|
if exist("DataTable",'var')
|
|
ListFiles = outerjoinRowToTable(ListFiles,DataTable);
|
|
end
|
|
% Then, we sort the table based on the time (datenum)
|
|
ListFiles = sortrows(ListFiles,'lastModifiedDatenum');
|
|
end |