source: aedes_readfdf.m @ 121

Last change on this file since 121 was 121, checked in by tjniskan, 9 years ago
  • Fixed contrast auto-balance to work with datas that are in [0 1] range
  • aedes_readfdf now reads also SWIFT reconstruction FDF:s

M aedes_readfdf.m
M aedes.m
M aedes_revision.m

File size: 4.4 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=double(fread(file_fid,2,'char'));
120  if any(tmp==0)
121    done=true;
122    continue;
123  else
124    % Move file indicator back
125    fseek(file_fid,-2,0);
126  end
127 
128  header{count}=fgetl(file_fid);
129end
130
131
132
133%% Parse the header cell array
134for ii=1:length(header)
135  if length(header{ii})<=2 || all(header{ii}==32) || ...
136        all(header{ii}==0)
137    continue;
138  end
139 
140  % Remove * and [] chars
141  header{ii}=strrep(header{ii},'[','');
142  header{ii}=strrep(header{ii},']','');
143  header{ii}=strrep(header{ii},'*','');
144 
145  % Replace " chars with '
146  header{ii}=strrep(header{ii},'"','''');
147 
148  %% Find the first space char
149  ind=find(header{ii}==32);
150  if length(ind)>1 && diff([ind(1:2)])==1
151    eval(['FileHeader.',header{ii}(ind(2)+1:end)])
152  else
153    eval(['FileHeader.',header{ii}(ind(1)+1:end)])
154  end
155end
156
157%% Check that numeric fields are numeric
158fldnames=fieldnames(FileHeader);
159for ii=1:length(fldnames)
160  if iscell(FileHeader.(fldnames{ii}))
161    if all(cellfun(@isnumeric,FileHeader.(fldnames{ii})))
162      tmp=FileHeader.(fldnames{ii});
163      FileHeader.(fldnames{ii}) = [tmp{:}];
164    end
165  end
166end
167
168catch
169  FileHeader=[];
170  msg='Error occurred while reading FDF file header!';
171end
172
173
174%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175% Read Data
176%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177function [data,msg]=l_ReadData(FileHeader,file_fid)
178
179data=[];
180msg='';
181
182%% Read data parameters
183data_sz = FileHeader.matrix;
184datatype = FileHeader.storage;
185bits = FileHeader.bits;
186dataformat=FileHeader.type;
187
188if strcmpi(datatype,'integer')
189  data_str = ['*int',num2str(bits)];
190elseif strcmpi(datatype,'float')
191  data_str = ['*float',num2str(bits)];
192end
193
194%% Read data
195[data,count]=fread(file_fid,inf,data_str);
196data=reshape(data,data_sz);
197
198% Permute to correct orientation
199data=permute(data,[2 1 3 4]);
200
201%for ii=1:size(data,3)
202%  data(:,:,ii)=rot90(data(:,:,ii));
203%end
204
Note: See TracBrowser for help on using the repository browser.

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