source: aedes_readfdf.m

Last change on this file was 124, checked in by tjniskan, 7 years ago
  • Fixed a bug in aedes_readfdf.m

M aedes_readfdf.m
M aedes_revision.m

File size: 4.5 KB
Line 
1function DATA = aedes_readfdf(filename,varargin)
2% AEDES_READFDF - Read Varian Flexible Data Format (FDF) Files
3%   
4%
5% Synopsis:
6%
7% Description:
8%
9% Examples:
10%
11% See also:
12%
13
14% This function is a part of Aedes - A graphical tool for analyzing
15% medical images
16%
17% Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi>
18%
19% Department of Physics, Department of Neurobiology
20% University of Kuopio, FINLAND
21%
22% This program may be used under the terms of the GNU General Public
23% License version 2.0 as published by the Free Software Foundation
24% and appearing in the file LICENSE.TXT included in the packaging of
25% this program.
26%
27% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29
30
31if nargin==0 || isempty(filename)
32  %% Ask for a file name
33  [f_name, f_path, f_index] = uigetfile( ...
34    {'*.fdf;*.FDF','Varian FDF-files (*.fdf)'; ...
35     '*.*', 'All Files (*.*)'}, ...
36    'Select Varian FDF-file');
37  if ( all(f_name==0) | all(f_path==0) ) % Cancel is pressed
38    DATA=[];
39    return
40  else
41    filename = [f_path,f_name];
42  end
43end
44
45% Try to open file for reading
46file_fid = fopen(filename,'r','ieee-le');
47if file_fid<0
48  DATA=[];
49  error('Could not open file "%s" for reading!',filename);
50end
51
52[fp,fn,fe]=fileparts(filename);
53
54% Read Header
55[FileHeader,msg]=l_ReadHdr(file_fid);
56if isempty(FileHeader)
57  DATA=[];
58  error(msg);
59end
60
61% Check endianness
62if isfield(FileHeader,'bigendian') && FileHeader.bigendian==1
63  pos = ftell(file_fid);
64  fclose(file_fid);
65  file_fid = fopen(filename,'r','ieee-be');
66  if file_fid<0
67    DATA=[];
68    error('Could not open file "%s" for reading!',filename);
69  end
70 
71  % Seek to data start
72  fseek(file_fid,pos,-1);
73end
74
75% Read Data
76[data,msg]=l_ReadData(FileHeader,file_fid);
77
78% Close file
79fclose(file_fid);
80
81% Construct DATA structure
82DATA.DataFormat = 'FDF';
83DATA.HDR.FileHeader = FileHeader;
84DATA.HDR.fname = [fn,fe];
85DATA.HDR.fpath = [fp,filesep];
86DATA.FTDATA = data;
87
88
89
90%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91% Read Data Header
92%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93function [FileHeader,msg]=l_ReadHdr(file_fid)
94
95FileHeader=[];
96msg = '';
97
98
99%% Check if file is a valid FDF-file
100magic_str=char(fread(file_fid,25,'char'))';
101if length(magic_str)<25 || ~strcmpi(magic_str(1:end-1),'#!/usr/local/fdf/startup')
102  msg='File is not a valid FDF-file!';
103  return
104end
105
106% Seek the file back to beginning
107
108try
109%% Read ASCII header to a cell array
110done=false;
111count=0;
112header={};
113while ~done
114  count=count+1;
115 
116  % Check first and second char on the line.
117  % In SWIFT reconstruction fdf:s there can be an additional
118  % form feed character (12) before NULL...
119  tmp=fread(file_fid,2,'uint8');
120  if any(tmp==0)
121    if tmp(1)==0
122      fseek(file_fid,-1,0);
123    end
124    done=true;
125    continue;
126  else
127    % Move file indicator back
128    fseek(file_fid,-2,0);
129  end
130 
131  header{count}=fgetl(file_fid);
132end
133
134
135
136%% Parse the header cell array
137for ii=1:length(header)
138  if length(header{ii})<=2 || all(header{ii}==32) || ...
139        all(header{ii}==0)
140    continue;
141  end
142 
143  % Remove * and [] chars
144  header{ii}=strrep(header{ii},'[','');
145  header{ii}=strrep(header{ii},']','');
146  header{ii}=strrep(header{ii},'*','');
147 
148  % Replace " chars with '
149  header{ii}=strrep(header{ii},'"','''');
150 
151  %% Find the first space char
152  ind=find(header{ii}==32);
153  if length(ind)>1 && diff([ind(1:2)])==1
154    eval(['FileHeader.',header{ii}(ind(2)+1:end)])
155  else
156    eval(['FileHeader.',header{ii}(ind(1)+1:end)])
157  end
158end
159
160%% Check that numeric fields are numeric
161fldnames=fieldnames(FileHeader);
162for ii=1:length(fldnames)
163  if iscell(FileHeader.(fldnames{ii}))
164    if all(cellfun(@isnumeric,FileHeader.(fldnames{ii})))
165      tmp=FileHeader.(fldnames{ii});
166      FileHeader.(fldnames{ii}) = [tmp{:}];
167    end
168  end
169end
170
171catch
172  FileHeader=[];
173  msg='Error occurred while reading FDF file header!';
174end
175
176
177%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178% Read Data
179%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180function [data,msg]=l_ReadData(FileHeader,file_fid)
181
182data=[];
183msg='';
184
185%% Read data parameters
186data_sz = FileHeader.matrix;
187datatype = FileHeader.storage;
188bits = FileHeader.bits;
189dataformat=FileHeader.type;
190
191if strcmpi(datatype,'integer')
192  data_str = ['*int',num2str(bits)];
193elseif strcmpi(datatype,'float')
194  data_str = ['*float',num2str(bits)];
195end
196
197%% Read data
198[data,count]=fread(file_fid,inf,data_str);
199data=reshape(data,data_sz);
200
201% Permute to correct orientation
202data=permute(data,[2 1 3 4]);
203
204%for ii=1:size(data,3)
205%  data(:,:,ii)=rot90(data(:,:,ii));
206%end
207
Note: See TracBrowser for help on using the repository browser.

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