Mercurial > hg > ltpda
comparison m-toolbox/classes/@ao/fromComplexDatafile.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 % FROMCOMPLEXDATAFILE Construct an AO from filename AND parameter list | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % FUNCTION: fromDatafile | |
5 % | |
6 % DESCRIPTION: Construct an AO from filename AND parameter list | |
7 % | |
8 % CALL: a = fromComplexDatafile(a, pli) | |
9 % | |
10 % PARAMETER: a: empty ao-object | |
11 % pli: plist-object (must contain the filename) | |
12 % | |
13 % VERSION: $Id: fromComplexDatafile.m,v 1.12 2011/08/12 09:55:39 hewitson Exp $ | |
14 % | |
15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
16 | |
17 function objs = fromComplexDatafile(ain, pli) | |
18 | |
19 utils.helper.msg(utils.const.msg.PROC1, 'loading complex data from filename and/or plist'); | |
20 | |
21 VERSION = '$Id: fromComplexDatafile.m,v 1.12 2011/08/12 09:55:39 hewitson Exp $'; | |
22 | |
23 % get AO info | |
24 mi = ao.getInfo('ao', 'From Complex ASCII File'); | |
25 | |
26 % Set the method version string in the minfo object | |
27 mi.setMversion([VERSION '-->' mi.mversion]); | |
28 | |
29 % Get filename | |
30 file_name = find(pli, 'filename'); | |
31 file_path = find(pli, 'filepath'); | |
32 | |
33 [filePath, fileName, ext] = fileparts(file_name); | |
34 | |
35 % Add the file extenstion to the fileName | |
36 fileName = strcat(fileName, ext); | |
37 | |
38 % Define the path of the | |
39 if ~isempty(file_path) && ~isempty(filePath) | |
40 % Do nothing because we will use filePath | |
41 elseif ~isempty(file_path) | |
42 filePath = file_path; | |
43 elseif ~isempty(filePath) | |
44 % Do nothing because we will use filePath | |
45 else | |
46 filePath = pwd(); | |
47 end | |
48 | |
49 absolutePathname = fullfile(filePath, fileName); | |
50 | |
51 % Check if the abolute Pathname exist | |
52 if ~exist(absolutePathname, 'file') | |
53 absolutePathname = fileName; | |
54 end | |
55 | |
56 %%%%%%%%%% Get default parameter list %%%%%%%%%% | |
57 pl = applyDefaults(mi.plists, pli); | |
58 | |
59 pl = pset(pl, 'filename', fileName); | |
60 pl = pset(pl, 'filepath', filePath); | |
61 | |
62 data_type = find (pl, 'type'); | |
63 columns = find (pl, 'columns'); | |
64 comment_char = find (pl, 'comment_char'); | |
65 orig_col = columns; % necessary for the histroy | |
66 objs = []; | |
67 | |
68 %%%%%%%%%% read file %%%%%%%%%% | |
69 [fid,msg] = fopen (absolutePathname, 'r'); | |
70 if (fid < 0) | |
71 error ('### can not open file: %s \n### error msg: %s', fileName, msg); | |
72 end | |
73 | |
74 %%%%%%%%%% create scan format: '%f %f %f %f %f %*[^\n]' %%%%%%%%%% | |
75 scan_format = ''; | |
76 read_col = 0; | |
77 sort_col = sort(columns); | |
78 | |
79 %%%%%%%%%% Get/Count max number of lines %%%%%%%%%% | |
80 maxLines = numlines(fid); | |
81 utils.helper.msg(utils.const.msg.PROC2, 'Counting lines: %d', maxLines); | |
82 fseek(fid, 0, 'bof'); | |
83 | |
84 %%%%%%%%%% Read data %%%%%%%%%% | |
85 readlines = min(50000, maxLines); | |
86 nlines = 0; | |
87 | |
88 %%% Based on skipping the not used columns we have to transform the columns. | |
89 %%% We must transform the columns [ 2 5 2 6 5 7] to [ 1 2 1 3 2 4] | |
90 %%% In each loop we have to replace the corresponding value. In the first loop | |
91 %%% the first minimum, in the second loop the second minimum, ... with the | |
92 %%% current loop number. | |
93 for j=1:max(columns) | |
94 if ismember(j, columns) | |
95 scan_format = [scan_format '%n ']; | |
96 read_col = read_col + 1; | |
97 replace = min(sort_col); | |
98 | |
99 columns (columns==replace) = read_col; | |
100 sort_col(sort_col==replace) = []; | |
101 else | |
102 scan_format = [scan_format '%*n ']; | |
103 end | |
104 end | |
105 scan_format = [deblank(scan_format) '%*[^\n]']; | |
106 | |
107 %%% preallocate data array | |
108 f_data = zeros(maxLines, read_col); | |
109 | |
110 %%% check if using robust read: 'yes'/'no' or true/false or 'true'/'false' | |
111 robust = find(pl, 'Robust'); | |
112 if isempty(robust) | |
113 robust = false; | |
114 elseif ischar(robust) | |
115 if strcmpi(robust, 'yes') || strcmpi(robust, 'true') | |
116 robust = true; | |
117 else | |
118 robust = false; | |
119 end | |
120 end | |
121 | |
122 if robust | |
123 | |
124 f_data = robustRead(fid, f_data, columns, orig_col); | |
125 | |
126 else | |
127 | |
128 %%% Look for the first line of data | |
129 if ~isempty(comment_char) | |
130 while ~feof(fid) | |
131 f = deblank(fgetl(fid)); | |
132 if ~isempty(f) | |
133 if f(1) ~= comment_char | |
134 break; | |
135 end | |
136 end | |
137 end | |
138 else | |
139 f = deblank(fgetl(fid)); | |
140 end | |
141 | |
142 %%% Scan it to find how many columns we have in the file | |
143 C = textscan(f, scan_format, 1, 'CollectOutput', 1); | |
144 if any(isnan(C{:})) | |
145 error('### Error in file format. Perhaps you specified more columns than the file contains?'); | |
146 end | |
147 | |
148 fseek(fid, 0, 'bof'); | |
149 %%% read file to end | |
150 while ~feof(fid) && nlines < maxLines | |
151 | |
152 if isempty(comment_char) | |
153 C = textscan(fid, scan_format, readlines, ... | |
154 'CollectOutput', 1); | |
155 else | |
156 C = textscan(fid, scan_format, readlines, ... | |
157 'CommentStyle', comment_char, ... | |
158 'CollectOutput', 1); | |
159 end | |
160 f_data(nlines+1:nlines+size(C{1},1),:) = C{1}; | |
161 nlines = nlines + length(C{1}); | |
162 | |
163 if isempty(C{1}) | |
164 error('\n### There are no data.\n### Did you use the right comment character?\n### The current comment character is: [%s]\n### Use a parameter list with the parameter:\n### plist(''comment_char'', ''%%'')', comment_char); | |
165 end | |
166 utils.helper.msg(utils.const.msg.PROC2, 'read %09d lines of %09d', nlines, maxLines); | |
167 | |
168 end | |
169 fclose(fid); | |
170 | |
171 %%% get only the data we want | |
172 if size(f_data,1) > nlines | |
173 f_data = f_data(1:nlines, :); | |
174 end | |
175 end | |
176 | |
177 | |
178 %%%%%%%%%% Create for each three columns the data object %%%%%%%%%% | |
179 | |
180 %%%%%%%%%% The numbers in columns must be straight %%%%%%%%%% | |
181 if mod(length(columns),3) ~= 0 | |
182 error('### The numbers in columns must be multiple of three.'); | |
183 end | |
184 | |
185 complex_type = pl.find('complex_type'); | |
186 | |
187 for lauf = 1:length(columns)/3 | |
188 | |
189 data_x = f_data(:, columns(lauf*3-2)); | |
190 data_y_1 = f_data(:, columns(lauf*3-1)); | |
191 data_y_2 = f_data(:, columns(lauf*3)); | |
192 | |
193 if strcmpi(complex_type, 'abs/deg') | |
194 data_y = data_y_1 .* exp(1i*data_y_2*pi/180); | |
195 | |
196 elseif strcmpi(complex_type, 'dB/deg') | |
197 data_y = 10.^(data_y_1./20) .* exp(1i*data_y_2*pi/180); | |
198 | |
199 elseif strcmpi(complex_type, 'abs/rad') | |
200 data_y = data_y_1 .* exp(1i*data_y_2); | |
201 | |
202 elseif strcmpi(complex_type, 'dB/rad') | |
203 data_y = 10.^(data_y_1./20) .* exp(1i*data_y_2); | |
204 | |
205 elseif strcmpi(complex_type, 'real/imag') | |
206 data_y = complex(data_y_1, data_y_2); | |
207 | |
208 else | |
209 error('### I can not handle real [%s] and imaginary [%s].', real_type, imag_type); | |
210 end | |
211 | |
212 % create data object corresponding to the parameter list | |
213 ao_data = []; | |
214 switch lower(data_type) | |
215 case 'tsdata' | |
216 ao_data = tsdata(data_x, data_y); | |
217 case 'fsdata' | |
218 ao_data = fsdata(data_x, data_y); | |
219 case 'cdata' | |
220 error('### Please code me up'); | |
221 case 'xydata' | |
222 ao_data = xydata(data_x, data_y); | |
223 otherwise | |
224 error('### unknown data type ''%s''', data_type); | |
225 end | |
226 aa = ao(ao_data); | |
227 % overide the default name | |
228 if isempty(pl.find('name')) | |
229 pl.pset('name', sprintf('%s_%02d_%02d_%02d', fileName, orig_col(lauf*3-2), orig_col(lauf*3-1), orig_col(lauf*3))); | |
230 end | |
231 | |
232 % set units | |
233 aa.setXunits(pl.find('xunits')); | |
234 aa.setYunits(pl.find('yunits')); | |
235 | |
236 % Add history | |
237 pl = pl.pset('columns', [orig_col(lauf*3-2) orig_col(lauf*3-1) orig_col(lauf*3)]); | |
238 aa.addHistory(mi, pl, [], []); | |
239 | |
240 objs = [objs aa]; | |
241 | |
242 end | |
243 | |
244 % set any object properties now | |
245 objs.setObjectProperties(pl); | |
246 | |
247 end | |
248 | |
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
250 % Local Functions % | |
251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
252 | |
253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
254 % | |
255 % FUNCTION: numlines | |
256 % | |
257 % SYNTAX: count = numlines(fid); | |
258 % | |
259 % DESCRIPTION: Number of lines in an ASCII file | |
260 % | |
261 % HISTORY: 02-08-2002 Peter Acklam, CSSM post | |
262 % Creation. | |
263 % | |
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
265 | |
266 function lines = numlines(fid) | |
267 | |
268 lines = 0; % number of lines in file | |
269 nlchr = uint8(sprintf('\n')); % newline chr as uint8 | |
270 bsize = 4 * 256 * 8192; % block size to read | |
271 | |
272 while ~feof(fid) | |
273 block = fread(fid, bsize, '*uint8'); | |
274 lines = lines + sum(block == nlchr); | |
275 end | |
276 if ~isempty(block) % in case file is empty | |
277 lines = lines + double(block(end) ~= nlchr); | |
278 end | |
279 end | |
280 | |
281 % A robust and slow data reader | |
282 function f_data = robustRead(fid, f_data, columns, orig_cols) | |
283 | |
284 cols = unique(columns); | |
285 ocols = unique(orig_cols); | |
286 Nline = 1; | |
287 while ~feof(fid) | |
288 % read and parse line | |
289 tokens = sscanf(fgets(fid), '%f'); | |
290 % parse tokens | |
291 if ~isempty(tokens) | |
292 f_data(Nline, cols) = tokens(ocols); | |
293 if mod(Nline, 1000) == 0 | |
294 utils.helper.msg(utils.const.msg.PROC2, 'lines read: %d', Nline); | |
295 end | |
296 Nline = Nline + 1; | |
297 end | |
298 end | |
299 | |
300 % drop empty lines | |
301 f_data = f_data(1:Nline-1, :); | |
302 | |
303 end |