Home > classes > @ao > fromDatafile.m

fromDatafile

PURPOSE ^

FROMDATAFILE Construct an ao from filename AND parameter list

SYNOPSIS ^

function a = fromDatafile(ain, pli)

DESCRIPTION ^

 FROMDATAFILE Construct an ao from filename AND parameter list
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 FUNCTION:    fromDatafile

 DESCRIPTION: Construct an ao from filename AND parameter list

 CALL:        a = fromFilenameAndPlist(a, pli)

 PARAMETER:   a:   empty ao-object
              pli: plist-object (must contain the filename)

 VERSION:     $Id: fromDatafile.m,v 1.5 2008/09/07 18:08:03 hewitson Exp $

 HISTORY:     07-05-2007 Hewitson
              Creation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 % FROMDATAFILE Construct an ao from filename AND parameter list
0002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0003 %
0004 % FUNCTION:    fromDatafile
0005 %
0006 % DESCRIPTION: Construct an ao from filename AND parameter list
0007 %
0008 % CALL:        a = fromFilenameAndPlist(a, pli)
0009 %
0010 % PARAMETER:   a:   empty ao-object
0011 %              pli: plist-object (must contain the filename)
0012 %
0013 % VERSION:     $Id: fromDatafile.m,v 1.5 2008/09/07 18:08:03 hewitson Exp $
0014 %
0015 % HISTORY:     07-05-2007 Hewitson
0016 %              Creation
0017 %
0018 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0019 
0020 function a = fromDatafile(ain, pli)
0021 
0022   utils.helper.msg(utils.const.msg.PROC1, 'constructing from filename and/or plist');
0023 
0024   VERSION = '$Id: fromDatafile.m,v 1.5 2008/09/07 18:08:03 hewitson Exp $';
0025 
0026   % get AO info
0027   mi = ao.getInfo('ao', 'From ASCII File');
0028 
0029   % Set the method version string in the minfo object
0030   mi.setMversion([VERSION '-->' mi.mversion]);
0031 
0032   % Get filename
0033   file_name = find(pli, 'filename');
0034 
0035   [pathstr, f_name, ext] = fileparts(file_name);
0036 
0037   %%%%%%%%%%   Get default parameter list   %%%%%%%%%%
0038   dpl = ao.getDefaultPlist('From ASCII File');
0039   pl  = combine(pli, dpl);
0040 
0041   pl = pset(pl, 'filename', [f_name ext]);
0042   pl = pset(pl, 'filepath', pathstr);
0043 
0044   data_type    = find (pl, 'type');
0045   columns      = find (pl, 'columns');
0046   maxLines     = find (pl,  'maxlines');
0047   comment_char = find (pl, 'comment_char');
0048   use_fs       = find (pl, 'use_fs');
0049   a            = [];
0050   orig_col     = columns; % necessary fro the histroy
0051 
0052   %%%%%%%%%%   read file   %%%%%%%%%%
0053   [fid,msg] = fopen (file_name, 'r');
0054   if (fid < 0)
0055     error ('### can not open file: %s \n\n### error msg:',file_name, msg);
0056   end
0057 
0058   %%%%%%%%%%   create scan format: '%f %f %f %f %f %*[^\n]'   %%%%%%%%%%
0059   maxcol      = max(columns);
0060   scan_format = '';
0061   read_col    = 0;
0062   sort_col    = sort(columns);
0063 
0064   %%%%%%%%%%   Get/Count max number of lines   %%%%%%%%%%
0065   if isempty(maxLines) || maxLines < 0
0066     maxLines = 1e20;
0067   end
0068   if maxLines == 1e20
0069     maxLines = numlines(fid);
0070     utils.helper.msg(utils.const.msg.PROC2, 'Counting lines: %d', maxLines);
0071   end
0072   fseek(fid, 0, 'bof');
0073 
0074   %%%%%%%%%%   Read data   %%%%%%%%%%
0075   readlines = min(50000, maxLines);
0076   nlines    = 0;
0077 
0078     %%% Based on skipping the not used columns we have to transform the columns.
0079     %%% We must transform the columns [ 2 5 2 6 5 7] to [ 1 2 1 3 2 4]
0080     %%% In each loop we have to replace the corresponding value. In the first loop
0081     %%% the first minimum, in the second loop the second minimum, ... with the
0082     %%% current loop number.
0083     for j=1:max(columns)
0084       if ismember(j, columns)
0085         scan_format = [scan_format '%n '];
0086         read_col = read_col + 1;
0087         replace = min(sort_col);
0088 
0089         columns (columns==replace)  = read_col;
0090         sort_col(sort_col==replace) = [];
0091       else
0092         scan_format = [scan_format '%*n '];
0093       end
0094     end
0095     scan_format = [deblank(scan_format) '%*[^\n]'];
0096     
0097   %%% preallocate data array
0098   f_data = zeros(maxLines, read_col);
0099 
0100   if strcmpi(find(pl, 'Robust'), 'yes')
0101     
0102     f_data = robustRead(fid, f_data, columns, orig_col);
0103     
0104   else
0105 
0106     
0107     %%% Look for the first line of data
0108     while ~feof(fid)
0109       f = deblank(fgetl(fid));
0110       if ~isempty(f)
0111         if f(1) ~= comment_char
0112           break;
0113         end
0114       end
0115     end
0116     %%% Scan it to find how many columns we have in the file
0117     C = textscan(f, scan_format, 1, ...
0118       'CommentStyle', comment_char, ...
0119       'CollectOutput', 1);
0120     if any(isnan(C{:}))
0121       error('### Error in file format. Perhaps you specified more columns than the file contains?');
0122     end
0123 
0124     fseek(fid, 0, 'bof');
0125     %%% read file to end
0126     while ~feof(fid) && nlines < maxLines
0127       C = textscan(fid, scan_format, readlines, ...
0128         'CommentStyle', comment_char, ...
0129         'CollectOutput', 1);
0130       f_data(nlines+1:nlines+size(C{1},1),:) = C{1};
0131       nlines = nlines + length(C{1});
0132 
0133       if isempty(C{1})
0134         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);
0135       end
0136 
0137       utils.helper.msg(utils.const.msg.PROC2, 'read %09d lines of %09d', nlines, maxLines);
0138     end
0139     fclose(fid);
0140 
0141     %%% get only the data we want
0142     if size(f_data,1) > nlines
0143       f_data = f_data(1:nlines, :);
0144     end
0145   end
0146   
0147   
0148   %%%%%%%%%%   Create for each column pair the data object   %%%%%%%%%%
0149   if isempty(use_fs)
0150 
0151     %%%%%%%%%%   The numbers in columns must be straight   %%%%%%%%%%
0152     if mod(length(columns),2) ~= 0
0153       error('### the numbers in columns must be straight');
0154     end
0155 
0156     for lauf = 1:length(columns)/2
0157 
0158       data_x_axes = f_data(:, columns(lauf*2-1));
0159       data_y_axes = f_data(:, columns(lauf*2));
0160 
0161       % create data object corresponding to the parameter list
0162       ao_data = [];
0163       switch data_type
0164         case 'tsdata'
0165           ao_data = tsdata( data_x_axes, data_y_axes);
0166         case 'fsdata'
0167           ao_data = fsdata( data_x_axes, data_y_axes);
0168         case 'cdata'
0169           error ('### please code me up')
0170         case 'xydata'
0171           ao_data = xydata( data_x_axes, data_y_axes);
0172         otherwise
0173           error('### unknown data type ''%s''', data_type);
0174       end
0175       aa = ao(ao_data);
0176       aa.setName(sprintf('%s_%02d_%02d', file_name, orig_col(lauf*2-1), orig_col(lauf*2)), 'internal');
0177       % Add history
0178       pl = pl.pset('columns', [orig_col(lauf*2-1) orig_col(lauf*2)]);
0179       aa.addHistory(mi, pl, [], []);
0180 
0181       % Set the description fron the parameter list or from the file
0182       description_pl = find(pl, 'description');
0183       if ~isempty(description_pl)
0184         aa.description = description_pl;
0185       end
0186 
0187       a = [a aa];
0188 
0189     end
0190 
0191     %%%%%%%%%%   Create for each column AND fs a data object   %%%%%%%%%%
0192   else % isempty(use_fs)
0193 
0194     for lauf = 1:length(columns)
0195 
0196       data_y_axes = f_data(:, columns(lauf));
0197 
0198       % create data object corresponding to the parameter list
0199       ao_data = [];
0200       switch data_type
0201         case 'tsdata'
0202           ao_data = tsdata(data_y_axes, use_fs);
0203         case 'fsdata'
0204           ao_data = fsdata(data_y_axes, use_fs);
0205         case 'cdata'
0206           error ('### please code me up')
0207         case 'xydata'
0208           error ('### please code me up')
0209         otherwise
0210           error('### unknown data type ''%s''', data_type);
0211       end
0212       aa = ao(ao_data);
0213       aa.setName(sprintf('%s_%02d', file_name, orig_col(lauf)), 'internal');
0214       pl = pl.pset('columns', orig_col(lauf));
0215       aa.addHistory(mi, pl, [], []);
0216 
0217       % Set the description fron the parameter list or from the file
0218       description_pl = find(pl, 'description');
0219       if ~isempty(description_pl)
0220         aa.description = description_pl;
0221       end
0222 
0223       a = [a aa];
0224 
0225     end
0226 
0227   end
0228 
0229   %%%%%%%%%%   set fields of the AO from the parameter list   %%%%%%%%%%
0230   for ii = 1:length(a)
0231 
0232     for jj = 1:pli.nparams
0233 
0234       cmd = ['set' pli.params(jj).key(1) lower(pli.params(jj).key(2:end))];
0235 
0236       if ismethod(a(ii), cmd)
0237         if iscell(pli.params(jj).val) && length(pli.params(jj).val) == length(a)
0238           %%% Set the value in the cell
0239           feval(cmd, a(ii), pli.params(jj).val{ii}, 'internal');
0240           %%% Reset the value in the plistUset to this single value
0241           a(ii).hist.plistUsed.pset(pli.params(jj).key, pli.params(jj).val{ii});
0242         else
0243           %%% Set the value
0244           feval(cmd, a(ii), pli.params(jj).val, 'internal');
0245         end
0246       end
0247 
0248     end
0249 
0250   end % for-loop over all ao's
0251 
0252 end
0253 
0254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0255 %                               Local Functions                               %
0256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0257 
0258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0259 %
0260 % FUNCTION:    numlines
0261 %
0262 % SYNTAX:      count = numlines(fid);
0263 %
0264 % DESCRIPTION: Number of lines in an ASCII file
0265 %
0266 % HISTORY:     02-08-2002 Peter Acklam, CSSM post
0267 %                 Creation.
0268 %
0269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0270 
0271 function lines = numlines(fid)
0272 
0273   lines = 0;                           % number of lines in file
0274   nlchr = uint8(sprintf('\n'));        % newline chr as uint8
0275   bsize = 4 * 256 * 8192;              % block size to read
0276 
0277   while ~feof(fid)
0278     block = fread(fid, bsize, '*uint8');
0279     lines = lines + sum(block == nlchr);
0280   end
0281   if ~isempty(block)                   % in case file is empty
0282     lines = lines + double(block(end) ~= nlchr);
0283   end
0284 end
0285 
0286 % A robust and slow data reader
0287 function f_data = robustRead(fid, f_data, columns, orig_cols)
0288     
0289   cols = unique(columns);
0290   ocols = unique(orig_cols);
0291   Nline = 1;
0292   while ~feof(fid)    
0293     % read and parse line
0294     tokens = sscanf(fgets(fid), '%f');
0295     % parse tokens
0296     if ~isempty(tokens)
0297       f_data(Nline, cols) = tokens(ocols);
0298       if mod(Nline, 1000) == 0
0299         utils.helper.msg(utils.const.msg.PROC2, 'lines read: %d', Nline);
0300       end
0301       Nline = Nline + 1;
0302     end        
0303   end
0304   
0305   % drop empty lines
0306   f_data = f_data(1:Nline-1, :);
0307     
0308 end

Generated on Mon 08-Sep-2008 13:18:47 by m2html © 2003