-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathaedes_getdataformat.m
More file actions
196 lines (177 loc) · 4.48 KB
/
aedes_getdataformat.m
File metadata and controls
196 lines (177 loc) · 4.48 KB
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
function dataformat = aedes_getdataformat(filename)
% AEDES_GETDATAFORMAT - Try to determine the data format of a file
%
%
% Synopsis:
% dataformat=aedes_getdataformat(filename);
%
% Description:
% Returns an identifier string corresponding to the data format of
% the file FILENAME. If the data format cannot be determined, an
% empty string is returned.
%
% The possible identifier strings are:
%
% 'vnmr' <-> Varian FID-file
% 'bruker_raw' <-> Bruker FID-file
% 'bruker_reco' <-> Bruker reconstructed 2dseq file
% 'nifti' <-> NIfTI or Analyze 7.5 format file
% 'sur' <-> S.M.I.S. SUR-File
% 'dcm' <-> DICOM File
% 'spect/ct' <-> Gamma Medica SPECT/CT File
% 'mat' <-> Matlab MAT-File
% 'roi' <-> Aedes ROI-File
% 'fdf' <-> Varian FDF-File
%
% Examples:
%
% See also:
% AEDES, AEDES_DATA_READ
% This function is a part of Aedes - A graphical tool for analyzing
% medical images
%
% Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi>
%
% Department of Physics, Department of Neurobiology
% University of Kuopio, FINLAND
%
% This program may be used under the terms of the GNU General Public
% License version 2.0 as published by the Free Software Foundation
% and appearing in the file LICENSE.TXT included in the packaging of
% this program.
%
% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
% Check number of arguments
if nargin==0
error('Too few input arguments')
elseif nargin>1
error('Too many input arguments')
end
dataformat = '';
[f_path,f_name,f_ext] = fileparts(filename);
if isempty(f_path)
f_path = [pwd,filesep];
else
f_path=[f_path,filesep];
end
% Check if is gzipped NIfTI
if strcmpi(f_ext,'.gz') && length(f_name)>3 && ...
strcmpi(f_name(end-3:end),'.nii')
f_name = f_name(1:length(f_name)-4);
f_ext = '.nii.gz';
end
if isempty(f_ext)
if strcmpi(f_name,'fid')
% Check if the file is a Bruker or Varian FID file
if exist([f_path,'procpar'],'file') == 2
dataformat = 'vnmr';
else
dataformat = 'bruker_raw';
end
return
elseif strcmpi(f_name,'2dseq')
dataformat = 'bruker_reco';
return
else
% Check if the file is a DICOM file which can many times be without
% file extension
fid = fopen(filename,'r');
if fid < 0
return
end
% Seek over the possible DICOM preamble
status = fseek(fid,128,-1);
if status == -1
% Unknown data format
fclose(fid);
return
end
% Try to read the 4 byte DICOM prefix
[str,count] = fread(fid,4,'char');
if count~=4
fclose(fid);
return
end
str = char(str.');
if strcmp(str,'DICM')
dataformat = 'dcm';
end
fclose(fid);
return
end
elseif strcmpi(f_ext,'.fid')
dataformat = 'vnmr';
else
if strcmpi(f_ext,'.xxm')
dataformat='spect/ct';
elseif any(strcmpi(f_ext,{'.nii','.nii.gz','.hdr','.img'}))
dataformat = 'nifti';
if strcmpi(f_ext,'.hdr')
% Check if data is in Analyze/NIfTI or SPECT/CT format
% Try to open the file for reading
fid = fopen(filename,'r');
if fid<0
return
end
% Read 10 characters from the start
[ident_str,count] = fread(fid,10,'char');
if count~=10
dataformat = '';
fclose(fid);
return
end
if strcmp(char(ident_str).','!INTERFILE')
dataformat = 'spect/ct';
else
dataformat = 'nifti';
end
end
elseif any(strcmpi(f_ext(2:end),...
{'t1r','s1r','t2r','s2r','t1','t2','s1','s2','df','sf','r2','b1'}))
% This file is probably Matlab MAT-file but could also be in the old
% S.M.I.S. SUR-Format...
fid = fopen(filename,'r');
if fid < 0
return
end
% The first 6 characters in MAT-File should be MATLAB
[str,count]=fread(fid,6,'char');
if count~=6
fclose(fid);
return
end
str = char(str).';
if strcmpi(str,'MATLAB')
dataformat = 'mat';
else
dataformat = 'sur';
end
elseif strcmpi(f_ext,'.sgl')
dataformat = 'swift_sgl';
else
dataformat = lower(f_ext(2:end)); % Remove the dot
% Check if file is a DICOM file
fid = fopen(filename,'r');
if fid < 0
return
end
% Seek over the possible DICOM preamble
status = fseek(fid,128,-1);
if status == -1
% Unknown data format
fclose(fid);
return
end
% Read the 4 byte DICOM prefix
[str,count] = fread(fid,4,'char');
fclose(fid);
if count~=4
return
end
str = char(str.');
if strcmp(str,'DICM')
dataformat = 'dcm';
end
end
end