forked from MRtrix3/mrtrix3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_mrtrix_tsf.m
106 lines (92 loc) · 2.52 KB
/
read_mrtrix_tsf.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
% Copyright (c) 2008-2019 the MRtrix3 contributors.
%
% This Source Code Form is subject to the terms of the Mozilla Public
% License, v. 2.0. If a copy of the MPL was not distributed with this
% file, You can obtain one at http://mozilla.org/MPL/2.0/.
%
% Covered Software is provided under this License on an "as is"
% basis, without warranty of any kind, either expressed, implied, or
% statutory, including, without limitation, warranties that the
% Covered Software is free of defects, merchantable, fit for a
% particular purpose or non-infringing.
% See the Mozilla Public License v. 2.0 for more details.
%
% For more details, see http://www.mrtrix.org/.
function tsf = read_mrtrix_tsf (filename)
% function: tsf = read_mrtrix_tsf (filename)
%
% returns a structure containing the header information and data for the MRtrix
% format tsf 'filename' (i.e. files with the extension '.tsf').
f = fopen (filename, 'r');
if (f<1)
disp (['error opening ' filename ]);
return
end
L = fgetl(f);
if ~strncmp(L, 'mrtrix track scalars', 20)
fclose(f);
disp ([filename ' is not in MRtrix format']);
return
end
tsf = struct();
while 1
L = fgetl(f);
if ~ischar(L), break, end;
L = strtrim(L);
if strcmp(L, 'END'), break, end;
d = strfind (L,':');
if isempty(d)
disp (['invalid line in header: ''' L ''' - ignored']);
else
key = lower(strtrim(L(1:d(1)-1)));
value = strtrim(L(d(1)+1:end));
if strcmp(key, 'file')
file = value;
elseif strcmp(key, 'datatype')
tsf.datatype = value;
else
tsf = add_field (tsf, key, value);
end
end
end
fclose(f);
if ~exist ('file') || ~isfield (tsf, 'datatype')
disp ('critical entries missing in header - aborting')
return
end
[ file, offset ] = strtok(file);
if ~strcmp(file,'.')
disp ('unexpected file entry (should be set to current ''.'') - aborting')
return;
end
if isempty(offset)
disp ('no offset specified - aborting')
return;
end
offset = str2num(char(offset));
datatype = lower(tsf.datatype);
byteorder = datatype(end-1:end);
if strcmp(byteorder, 'le')
f = fopen (filename, 'r', 'l');
datatype = datatype(1:end-2);
elseif strcmp(byteorder, 'be')
f = fopen (filename, 'r', 'b');
datatype = datatype(1:end-2);
else
disp ('unexpected data type - aborting')
return;
end
if (f<1)
disp (['error opening ' filename ]);
return
end
fseek (f, offset, -1);
data = fread(f, inf, datatype);
fclose (f);
k = find (~isfinite(data(:,1)));
tsf.data = {};
pk = 1;
for n = 1:(prod(size(k)))
tsf.data{end+1} = data(pk:(k(n)-1),:);
pk = k(n)+1;
end