-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrukerRead.m
executable file
·297 lines (234 loc) · 7.99 KB
/
BrukerRead.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
function varargout = BrukerRead(varargin)
%BRUKERREAD Load Bruker BE3ST files (.DTA / .DSC / .YGF).
%
% BRUKERREAD()
% BRUKERREAD('/path/to/file')
% dset = BRUKERREAD(...)
% [x, o, pars] = BRUKERREAD(...)
%
% BRUKERREAD when run without any inputs, opens a GUI so that the user can
% open the file themselves. BRUKERREAD can also accept a path to a file as
% an input if the path is put in 'quotes' and the extension (.DTA) is left
% off.
%
% SYNTAX:
% [x, o, pars] = BrukerRead
% GUI load a file
%
% dset = BrukerRead
% GUI load a file, return as a table
%
% [x, o, pars] = BrukerRead('/path/to/file.DSC')
% load x,o and info of file.DSC to the workspace
%
% INPUT:
% input1 - a string input to the path of a file
%
% OUTPUT:
% x - x-axis
% o - ordinate: the measured quantity
% pars - array of information about the loaded file
%
% OUTPUT:
% dset - a Matlab table with the measurement data
%
%% Input arguments
% ========================================================================
% remember file path from previous function calls
global Path
switch nargin
case 0
if Path==0; Path=[]; end
[file, directory] = uigetfile({'*.DSC;', 'Bruker File (*.DSC)'; '*.*', 'All Files (*.*)'}, 'Load Bruker file', Path);
Path = directory;
% if user cancels command nothing happens
if isequal(file, 0) || isequal(directory, 0)
Path = [];
return
end
% File name/path manipulation
address = [directory, file];
[~, name, ~] = fileparts(address);
case 1
address = varargin{1};
[directory, name, ~] = fileparts(address);
end
%% Parameter file
% ========================================================================
% Load .dsc file
file_dsc = fullfile(directory, strcat(name, ".DSC"));
fid = fopen(file_dsc, 'r');
if fid < 0
error('Both *.DSC and *.DTA files are required to open the file.')
end
% Get characters from file
string = fscanf(fid , '%c');
% Close file, free up memory
fclose(fid);
% Convert character array into useful string array (insert line breaks)
lines = strsplit(string, '\n');
parameter_list = char(lines);
% Do basic reading of parameter file
pars = param2struct(parameter_list);
%% Data file
% ========================================================================
% Load .dta file
file_dta = fullfile(directory, strcat(name, ".DTA"));
fid = fopen(file_dta, 'r', 'ieee-be.l64');
if fid < 0
error(['File ''', name, '.DTA'' could not be opened, both *.DTA and *.DSC files are required to open the file.'])
end
[dta, ~] = fread(fid, inf, 'float64'); % TODO: add support for other formats
fclose(fid);
yNum = length(pars.IKKF); % number of datasets per slice scan
yNumCplx = length(pars.IKKF(pars.IKKF == "CPLX"));
yNumTotal = yNum + yNumCplx;
o = [];
for i = 1:yNumTotal
o = [o dta(i:yNumTotal:end)];
end
%% X-Axes
% ========================================================================
if strcmp(pars.XTYP, 'IDX') % indexed data
x = linspace(pars.XMIN, pars.XMIN + pars.XWID, pars.XPTS)';
elseif strcmp(pars.XTYP, 'IGD') % data points saved in file
% if exist, load .YGF , convert to usable matrix
file_xgf = fullfile(directory, [name '.XGF']);
fid = fopen(file_xgf, 'r', 'ieee-be.l64');
if fid < 0
error('*.XGF file expected but not found.')
end
x = fread(fid, inf, 'float64');
else
x = [];
end
%% Y-Axes
% ========================================================================
if strcmp(pars.YTYP, 'IDX') % indexed data
y = linspace(pars.YMIN, pars.YMIN + pars.YWID, pars.YPTS)';
elseif strcmp(pars.YTYP, 'IGD') % data points saved in file
% if exist, load .YGF , convert to usable matrix
file_ygf = fullfile(directory, strcat(name, ".YGF"));
fid = fopen(file_ygf, 'r', 'ieee-be.l64');
if fid < 0
error('*.YGF file expected but not found.')
end
y = fread(fid, inf, 'float64');
else
y = [];
end
%% Z-Axes
% ========================================================================
if strcmp(pars.ZTYP, 'IDX') % indexed data
z = linspace(pars.ZMIN, pars.ZMIN + pars.ZWID, pars.ZPTS)';
elseif strcmp(pars.ZTYP, 'IGD') % data points saved in file
% if exist, load .ZGF , convert to usable matrix
file_zgf = fullfile(directory, [name '.ZGF']);
fid = fopen(file_zgf, 'r', 'ieee-be.l64');
if fid < 0
error('*.ZGF file expected but not found.')
end
z = fread(fid, inf, 'float64');
else
z = [];
end
%% Reshape data
% ========================================================================
dset = table();
for i=1:size(o, 2)
if ~isempty(z)
dset.(join(['o', num2str(i)])) = reshape(o(:,i), length(x), length(y), length(z));
elseif ~isempty(y)
dset.(join(['o', num2str(i)])) = reshape(o(:,i), length(x), length(y));
else
dset.(join(['o', num2str(i)])) = o(:,i);
end
end
%% Output arguments
% ========================================================================
dset = [table(x) dset];
yUnits = {};
for k = 1:length(pars.IKKF)
if strcmp(pars.IKKF{k}, 'CPLX')
yUnits(end+1:end+2) = {pars.IRUNI{k}; pars.IRUNI{k}};
else
yUnits(end+1) = {pars.IRUNI{k}};
end
end
pars.x_axis = x;
pars.y_axis = y;
pars.z_axis = z;
pars.path = file_dsc;
dset.Properties.VariableUnits = [{pars.XUNI} yUnits];
dset.Properties.UserData = pars;
% Output results according to requests
switch nargout
case 1
varargout{1} = dset;
case 3
varargout{1} = x;
varargout{2} = dset{:,2:end};
varargout{3} = pars;
otherwise
varargout{1} = dset;
end
end
function [par_struct] = param2struct(par_list)
%% generate info structure
% get number of rows
[N, ~] = size(par_list);
% preallocate memory for analysed rows
Keep = zeros(N, 1);
ParaMatrix = cell(N, 2);
% separate parameter names from values and save both in array
for i = 1:N
[parameter, value] = strtok(par_list(i, :));
value = strtrim(value);
ParaMatrix{i, 1} = parameter;
REGEX = '{(?<ndmin>\d*);(?<shape>[\d,]*);(?<default>[0-9\.e+-]*)\[?(?<unit>\w*)\]?}\s(?<value>.*)';
matrix_match = regexp(value, REGEX, 'names');
% if value is a matrix, convert
if ~isempty(matrix_match)
value = str2num(matrix_match.value);
shape = str2num(matrix_match.shape);
if length(shape) > 1
ParaMatrix{i, 2} = reshape(value, shape);
else
ParaMatrix{i, 2} = value;
end
% convert numeric values to double
elseif ~isnan(str2double(value))
ParaMatrix{i, 2} = str2double(value);
% keep everything else as string
else
value = strip(value, 'both', "'"); % strip single quotes
ParaMatrix{i, 2} = value;
end
% Mark rows that do not contain acquisition parameters.
% We recognise them because they start with non-letter characters
% or are empty
if isempty(parameter)
Keep(i) = 0;
else
isvar = isletter(parameter);
Keep(i) = isvar(1);
end
end
% delete rows that do not contain acquisition parameters
ParaMatrix = ParaMatrix(Keep==1, :);
% save all parameters in info structure
for i = 1:length(ParaMatrix)
name = matlab.lang.makeValidName(ParaMatrix{i, 1});
par_struct.(name) = ParaMatrix{i, 2};
end
% convert string arrays
for name = ["IKKF", "IRFMT", "IRNAM", "IRUNI"]
par_struct.(name) = strip(split(par_struct.(name), ','), 'both', "'");
end
% replace empty units by a.u.
for k=1:length(par_struct.IRUNI)
if isempty(par_struct.IRUNI{k})
par_struct.IRUNI{k} = 'a.u.';
end
end
end