1 | function dataformat = aedes_getdataformat(filename) |
---|
2 | % AEDES_GETDATAFORMAT - Try to determine the data format of a file |
---|
3 | % |
---|
4 | % |
---|
5 | % Synopsis: |
---|
6 | % dataformat=aedes_getdataformat(filename); |
---|
7 | % |
---|
8 | % Description: |
---|
9 | % Returns an identifier string corresponding to the data format of |
---|
10 | % the file FILENAME. If the data format cannot be determined, an |
---|
11 | % empty string is returned. |
---|
12 | % |
---|
13 | % The possible identifier strings are: |
---|
14 | % |
---|
15 | % 'vnmr' <-> Varian FID-file |
---|
16 | % 'bruker_raw' <-> Bruker FID-file |
---|
17 | % 'bruker_reco' <-> Bruker reconstructed 2dseq file |
---|
18 | % 'nifti' <-> NIfTI or Analyze 7.5 format file |
---|
19 | % 'sur' <-> S.M.I.S. SUR-File |
---|
20 | % 'dcm' <-> DICOM File |
---|
21 | % 'spect/ct' <-> Gamma Medica SPECT/CT File |
---|
22 | % 'mat' <-> Matlab MAT-File |
---|
23 | % 'roi' <-> Aedes ROI-File |
---|
24 | % 'fdf' <-> Varian FDF-File |
---|
25 | % |
---|
26 | % Examples: |
---|
27 | % |
---|
28 | % See also: |
---|
29 | % AEDES, AEDES_DATA_READ |
---|
30 | |
---|
31 | % This function is a part of Aedes - A graphical tool for analyzing |
---|
32 | % medical images |
---|
33 | % |
---|
34 | % Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi> |
---|
35 | % |
---|
36 | % Department of Physics, Department of Neurobiology |
---|
37 | % University of Kuopio, FINLAND |
---|
38 | % |
---|
39 | % This program may be used under the terms of the GNU General Public |
---|
40 | % License version 2.0 as published by the Free Software Foundation |
---|
41 | % and appearing in the file LICENSE.TXT included in the packaging of |
---|
42 | % this program. |
---|
43 | % |
---|
44 | % This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
---|
45 | % WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
---|
46 | |
---|
47 | % Check number of arguments |
---|
48 | if nargin==0 |
---|
49 | error('Too few input arguments') |
---|
50 | elseif nargin>1 |
---|
51 | error('Too many input arguments') |
---|
52 | end |
---|
53 | |
---|
54 | dataformat = ''; |
---|
55 | |
---|
56 | [f_path,f_name,f_ext] = fileparts(filename); |
---|
57 | if isempty(f_path) |
---|
58 | f_path = [pwd,filesep]; |
---|
59 | else |
---|
60 | f_path=[f_path,filesep]; |
---|
61 | end |
---|
62 | |
---|
63 | % Check if is gzipped NIfTI |
---|
64 | if strcmpi(f_ext,'.gz') && length(f_name)>3 && ... |
---|
65 | strcmpi(f_name(end-3:end),'.nii') |
---|
66 | f_name = f_name(1:length(f_name)-4); |
---|
67 | f_ext = '.nii.gz'; |
---|
68 | end |
---|
69 | |
---|
70 | if isempty(f_ext) |
---|
71 | if strcmpi(f_name,'fid') |
---|
72 | % Check if the file is a Bruker or Varian FID file |
---|
73 | if exist([f_path,'procpar'],'file') == 2 |
---|
74 | dataformat = 'vnmr'; |
---|
75 | else |
---|
76 | dataformat = 'bruker_raw'; |
---|
77 | end |
---|
78 | return |
---|
79 | elseif strcmpi(f_name,'2dseq') |
---|
80 | dataformat = 'bruker_reco'; |
---|
81 | return |
---|
82 | else |
---|
83 | % Check if the file is a DICOM file which can many times be without |
---|
84 | % file extension |
---|
85 | fid = fopen(filename,'r'); |
---|
86 | if fid < 0 |
---|
87 | return |
---|
88 | end |
---|
89 | |
---|
90 | % Seek over the possible DICOM preamble |
---|
91 | status = fseek(fid,128,-1); |
---|
92 | if status == -1 |
---|
93 | % Unknown data format |
---|
94 | fclose(fid); |
---|
95 | return |
---|
96 | end |
---|
97 | |
---|
98 | % Try to read the 4 byte DICOM prefix |
---|
99 | [str,count] = fread(fid,4,'char'); |
---|
100 | if count~=4 |
---|
101 | fclose(fid); |
---|
102 | return |
---|
103 | end |
---|
104 | str = char(str.'); |
---|
105 | if strcmp(str,'DICM') |
---|
106 | dataformat = 'dcm'; |
---|
107 | end |
---|
108 | fclose(fid); |
---|
109 | return |
---|
110 | end |
---|
111 | elseif strcmpi(f_ext,'.fid') |
---|
112 | dataformat = 'vnmr'; |
---|
113 | else |
---|
114 | if strcmpi(f_ext,'.xxm') |
---|
115 | dataformat='spect/ct'; |
---|
116 | elseif any(strcmpi(f_ext,{'.nii','.nii.gz','.hdr','.img'})) |
---|
117 | dataformat = 'nifti'; |
---|
118 | if strcmpi(f_ext,'.hdr') |
---|
119 | % Check if data is in Analyze/NIfTI or SPECT/CT format |
---|
120 | |
---|
121 | % Try to open the file for reading |
---|
122 | fid = fopen(filename,'r'); |
---|
123 | if fid<0 |
---|
124 | return |
---|
125 | end |
---|
126 | |
---|
127 | % Read 10 characters from the start |
---|
128 | [ident_str,count] = fread(fid,10,'char'); |
---|
129 | if count~=10 |
---|
130 | dataformat = ''; |
---|
131 | fclose(fid); |
---|
132 | return |
---|
133 | end |
---|
134 | |
---|
135 | if strcmp(char(ident_str).','!INTERFILE') |
---|
136 | dataformat = 'spect/ct'; |
---|
137 | else |
---|
138 | dataformat = 'nifti'; |
---|
139 | end |
---|
140 | end |
---|
141 | elseif any(strcmpi(f_ext(2:end),... |
---|
142 | {'t1r','s1r','t2r','s2r','t1','t2','s1','s2','df','sf','r2','b1'})) |
---|
143 | % This file is probably Matlab MAT-file but could also be in the old |
---|
144 | % S.M.I.S. SUR-Format... |
---|
145 | fid = fopen(filename,'r'); |
---|
146 | if fid < 0 |
---|
147 | return |
---|
148 | end |
---|
149 | |
---|
150 | % The first 6 characters in MAT-File should be MATLAB |
---|
151 | [str,count]=fread(fid,6,'char'); |
---|
152 | if count~=6 |
---|
153 | fclose(fid); |
---|
154 | return |
---|
155 | end |
---|
156 | str = char(str).'; |
---|
157 | if strcmpi(str,'MATLAB') |
---|
158 | dataformat = 'mat'; |
---|
159 | else |
---|
160 | dataformat = 'sur'; |
---|
161 | end |
---|
162 | elseif strcmpi(f_ext,'.sgl') |
---|
163 | dataformat = 'swift_sgl'; |
---|
164 | else |
---|
165 | dataformat = lower(f_ext(2:end)); % Remove the dot |
---|
166 | |
---|
167 | % Check if file is a DICOM file |
---|
168 | fid = fopen(filename,'r'); |
---|
169 | if fid < 0 |
---|
170 | return |
---|
171 | end |
---|
172 | |
---|
173 | % Seek over the possible DICOM preamble |
---|
174 | status = fseek(fid,128,-1); |
---|
175 | if status == -1 |
---|
176 | % Unknown data format |
---|
177 | fclose(fid); |
---|
178 | return |
---|
179 | end |
---|
180 | |
---|
181 | % Read the 4 byte DICOM prefix |
---|
182 | [str,count] = fread(fid,4,'char'); |
---|
183 | fclose(fid); |
---|
184 | if count~=4 |
---|
185 | return |
---|
186 | end |
---|
187 | str = char(str.'); |
---|
188 | if strcmp(str,'DICM') |
---|
189 | dataformat = 'dcm'; |
---|
190 | end |
---|
191 | |
---|
192 | end |
---|
193 | |
---|
194 | |
---|
195 | end |
---|
196 | |
---|