From 7633f3a7414fd252b3c490ee9160730b0107e430 Mon Sep 17 00:00:00 2001 From: ThorBarRa Date: Thu, 23 May 2024 07:16:38 +0200 Subject: [PATCH] First version import-export to h5 file --- import/WriteH5.m | 134 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 import/WriteH5.m diff --git a/import/WriteH5.m b/import/WriteH5.m new file mode 100644 index 0000000..5132e64 --- /dev/null +++ b/import/WriteH5.m @@ -0,0 +1,134 @@ +%% Develpment of hdf export and import +% +% from table to our instrument hdf structure +%% Todo +% +% * ensure logging of processing in NEXAFS and XPS to select data, make it +% usfull for string operations (no cells) +% * How to include units and definitions +% * Next, make this work for averageded files +%% +clc +% % add some SES data +%tmpFileImport = load('../beamtimes/20220828BeamTimeIceNappIss/FileList.mat'); +% FileList = outerjoinRowToTable(FileList, tmpFileImport.FileList); +clearvars tmp* +% select only data that has been used, +[tmpn1, ~] = cellfun(@size,FileList.spectrum_counts); % spectra have been generated +[tmpn2, ~] = cellfun(@size,FileList.spectrum_countsNorm); +[tmpn3, ~] = cellfun(@size,FileList.spectrum_countsPerSecond); +tmpDataSelection = contains(FileList.sample,lettersPattern) ... % we have added sample notes, so this is data + & ~contains(FileList.sample, '(bad data)') & ~contains(FileList.sample, '(no notes)') ... + & (tmpn1 > 1 | tmpn2 > 1 | tmpn3 > 1); % has actually been proceesed. + + + +tmpUserName = 'ThorstenBR'; +tmpFileList = FileList(tmpDataSelection,:); +% get table variables that are data +tmpMissingVariable = ~ismember(tmpFileList.Properties.VariableNames,... + [{'image'}; {'spectrum_counts'}; {'spectrum_countsPerSecond'}; {'spectrum_countsNorm'}; {'xRayEkin_eV'}; {'scientaEkin_eV'}; {'imageRoh'}; {'spectrum_countsPerSecondRoh'};{'beamlineInt'}]); +tmpVariablesToBeAdded = tmpFileList.Properties.VariableNames(tmpMissingVariable); + +for ij = 1:size(tmpFileList,1) + % get file name and "instruement" name + tmpPathParts = split(tmpFileList(ij,:).folder,"/"); + tmpFileName = [tmpPathParts{end-1} tmpUserName '.h5']; + tmpTopGroupName = tmpPathParts{end}; + % write data + h5create(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/image'],... + size(tmpFileList(ij,:).image{:})); + h5write(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/image'],... + tmpFileList(ij,:).image{:}) + if %XPS + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/image'],'label','electron emission intensity'); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/image'],'units','XPS (y,x): counts per second vs. electron volt; NEXAFS (y,x,z): counts vs. X-ray photon kinetic energy vs. electron kinetic energy'); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/image'],'definition','X-ray excited electron emission'); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/image'],'comment','XPS: intensity vs. electron kinetic energy for each sequence iteration; NEXAFS: intensity vs. x-ray photon kinetic energy (non-linear scaling) vs. electron kinetic energy'); + else %NEXAFS + + end + + try + h5create(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_counts'],... + size(tmpFileList(ij,:).spectrum_counts{:})); + h5write(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_counts'],... + tmpFileList(ij,:).spectrum_counts{:}) + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_counts'],'label','electron emission intensity'); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_counts'],'units','arb. unit'); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_counts'],'definition','X-ray excited electron emission'); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_counts'],'comment',' '); + catch + end + try + h5create(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_countsPerSecond'],... + size(tmpFileList(ij,:).spectrum_countsPerSecond{:})); + h5write(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_countsPerSecond'],... + tmpFileList(ij,:).spectrum_countsPerSecond{:}) + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_countsPerSecond'],'label',' '); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_countsPerSecond'],'units',' '); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_countsPerSecond'],'definition',' '); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_countsPerSecond'],'comment',' '); + catch + end + try + h5create(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_countsNorm'],... + size(tmpFileList(ij,:).spectrum_countsNorm{:})); + h5write(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_countsNorm'],... + tmpFileList(ij,:).spectrum_countsNorm{:}) + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_countsNorm'],'label',' '); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_countsNorm'],'units',' '); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_countsNorm'],'definition',' '); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/spectrum_countsNorm'],'comment',' '); + catch + end + try + h5create(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/beamlineInt'],... + size(tmpFileList(ij,:).beamlineInt{:})); + h5write(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/beamlineInt'],... + tmpFileList(ij,:).beamlineInt{:}) + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/beamlineInt'],'label',' '); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/beamlineInt'],'units',' '); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/beamlineInt'],'definition',' '); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/beamlineInt'],'comment',' '); + catch + end + h5create(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/xRayEkin_eV'],... + size(tmpFileList(ij,:).xRayEkin_eV{:})); + h5write(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/xRayEkin_eV'],... + tmpFileList(ij,:).xRayEkin_eV{:}) + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/xRayEkin_eV'],'label',' '); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/xRayEkin_eV'],'units',' '); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/xRayEkin_eV'],'definition',' '); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/xRayEkin_eV'],'comment',' '); + + h5create(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/scientaEkin_eV'],... + size(tmpFileList(ij,:).scientaEkin_eV{:})); + h5write(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/scientaEkin_eV'],... + tmpFileList(ij,:).scientaEkin_eV{:}) + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/scientaEkin_eV'],'label',' '); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/scientaEkin_eV'],'units',' '); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/scientaEkin_eV'],'definition',' '); + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:} '/scientaEkin_eV'],'comment',' '); + % add attributes + tmpCells= []; % create empty array + for ii = 1:size(tmpVariablesToBeAdded,2) + + if matches(class(tmpFileList.(tmpVariablesToBeAdded{ii})),'cell') + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:}],tmpVariablesToBeAdded{ii},tmpFileList(ij,:).(tmpVariablesToBeAdded{ii}){:}); + tmpCells = [tmpCells,', ' tmpVariablesToBeAdded{ii}]; + else + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:}],tmpVariablesToBeAdded{ii},tmpFileList(ij,:).(tmpVariablesToBeAdded{ii})); + end + % write variables that were cells to a list; so upon import the + % variable type can be set again. + h5writeatt(tmpFileName,['/' tmpTopGroupName '/' tmpFileList(ij,:).name{:}],'matlabVariablesThatAreCells',tmpCells); + + end + clearvars tmpCells + + +end +disp('*************************************************') +disp('* Voila: HDF file(s) written *') +disp('*************************************************')