source: aedes_getdataformat.m @ 131

Last change on this file since 131 was 80, checked in by tjniskan, 10 years ago
  • Changed the historical "an2_" prefix to "aedes_" in all files. NOTE:

Any script or function relying to Aedes functions will be broken
because of this. Just do a search/replace from "an2_" to "aedes_" in
your files and all should be well...

  • Changed the name of an2_readtab.m to a more informative

aedes_readphasetable.m

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

Powered by Trac 1.0.9.Copyright © Juha-Pekka Niskanen 2008