-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathaedes_readfdf.m
More file actions
207 lines (169 loc) · 4.48 KB
/
Copy pathaedes_readfdf.m
File metadata and controls
207 lines (169 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
196
197
198
199
200
201
202
203
204
205
206
function DATA = aedes_readfdf(filename,varargin)
% AEDES_READFDF - Read Varian Flexible Data Format (FDF) Files
%
%
% Synopsis:
%
% Description:
%
% Examples:
%
% See also:
%
% 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.
if nargin==0 || isempty(filename)
%% Ask for a file name
[f_name, f_path, f_index] = uigetfile( ...
{'*.fdf;*.FDF','Varian FDF-files (*.fdf)'; ...
'*.*', 'All Files (*.*)'}, ...
'Select Varian FDF-file');
if ( all(f_name==0) | all(f_path==0) ) % Cancel is pressed
DATA=[];
return
else
filename = [f_path,f_name];
end
end
% Try to open file for reading
file_fid = fopen(filename,'r','ieee-le');
if file_fid<0
DATA=[];
error('Could not open file "%s" for reading!',filename);
end
[fp,fn,fe]=fileparts(filename);
% Read Header
[FileHeader,msg]=l_ReadHdr(file_fid);
if isempty(FileHeader)
DATA=[];
error(msg);
end
% Check endianness
if isfield(FileHeader,'bigendian') && FileHeader.bigendian==1
pos = ftell(file_fid);
fclose(file_fid);
file_fid = fopen(filename,'r','ieee-be');
if file_fid<0
DATA=[];
error('Could not open file "%s" for reading!',filename);
end
% Seek to data start
fseek(file_fid,pos,-1);
end
% Read Data
[data,msg]=l_ReadData(FileHeader,file_fid);
% Close file
fclose(file_fid);
% Construct DATA structure
DATA.DataFormat = 'FDF';
DATA.HDR.FileHeader = FileHeader;
DATA.HDR.fname = [fn,fe];
DATA.HDR.fpath = [fp,filesep];
DATA.FTDATA = data;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read Data Header
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [FileHeader,msg]=l_ReadHdr(file_fid)
FileHeader=[];
msg = '';
%% Check if file is a valid FDF-file
magic_str=char(fread(file_fid,25,'char'))';
if length(magic_str)<25 || ~strcmpi(magic_str(1:end-1),'#!/usr/local/fdf/startup')
msg='File is not a valid FDF-file!';
return
end
% Seek the file back to beginning
try
%% Read ASCII header to a cell array
done=false;
count=0;
header={};
while ~done
count=count+1;
% Check first and second char on the line.
% In SWIFT reconstruction fdf:s there can be an additional
% form feed character (12) before NULL...
tmp=fread(file_fid,2,'uint8');
if any(tmp==0)
if tmp(1)==0
fseek(file_fid,-1,0);
end
done=true;
continue;
else
% Move file indicator back
fseek(file_fid,-2,0);
end
header{count}=fgetl(file_fid);
end
%% Parse the header cell array
for ii=1:length(header)
if length(header{ii})<=2 || all(header{ii}==32) || ...
all(header{ii}==0)
continue;
end
% Remove * and [] chars
header{ii}=strrep(header{ii},'[','');
header{ii}=strrep(header{ii},']','');
header{ii}=strrep(header{ii},'*','');
% Replace " chars with '
header{ii}=strrep(header{ii},'"','''');
%% Find the first space char
ind=find(header{ii}==32);
if length(ind)>1 && diff([ind(1:2)])==1
eval(['FileHeader.',header{ii}(ind(2)+1:end)])
else
eval(['FileHeader.',header{ii}(ind(1)+1:end)])
end
end
%% Check that numeric fields are numeric
fldnames=fieldnames(FileHeader);
for ii=1:length(fldnames)
if iscell(FileHeader.(fldnames{ii}))
if all(cellfun(@isnumeric,FileHeader.(fldnames{ii})))
tmp=FileHeader.(fldnames{ii});
FileHeader.(fldnames{ii}) = [tmp{:}];
end
end
end
catch
FileHeader=[];
msg='Error occurred while reading FDF file header!';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read Data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [data,msg]=l_ReadData(FileHeader,file_fid)
data=[];
msg='';
%% Read data parameters
data_sz = FileHeader.matrix;
datatype = FileHeader.storage;
bits = FileHeader.bits;
dataformat=FileHeader.type;
if strcmpi(datatype,'integer')
data_str = ['*int',num2str(bits)];
elseif strcmpi(datatype,'float')
data_str = ['*float',num2str(bits)];
end
%% Read data
[data,count]=fread(file_fid,inf,data_str);
data=reshape(data,data_sz);
% Permute to correct orientation
data=permute(data,[2 1 3 4]);
%for ii=1:size(data,3)
% data(:,:,ii)=rot90(data(:,:,ii));
%end