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.2 2008/08/25 12:43:10 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.2 2008/08/25 12:43:10 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.2 2008/08/25 12:43:10 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   %%% Based on skipping the not used columns we have to transform the columns.
0065   %%% We must transform the columns [ 2 5 2 6 5 7] to [ 1 2 1 3 2 4]
0066   %%% In each loop we have to replace the corresponding value. In the first loop
0067   %%% the first minimum, in the second loop the second minimum, ... with the
0068   %%% current loop number.
0069   for j=1:max(columns)
0070     if ismember(j, columns)
0071       scan_format = [scan_format '%n '];
0072       read_col = read_col + 1;
0073       replace = min(sort_col);
0074 
0075       columns (columns==replace)  = read_col;
0076       sort_col(sort_col==replace) = [];
0077     else
0078       scan_format = [scan_format '%*n '];
0079     end
0080   end
0081   if maxcol == 1
0082     scan_format = [deblank(scan_format) '%*[^\n]'];
0083   end
0084   
0085   %%%%%%%%%%   Get/Count max number of lines   %%%%%%%%%%
0086   if isempty(maxLines) || maxLines < 0
0087     maxLines = 1e20;
0088   end
0089   if maxLines == 1e20
0090     maxLines = numlines(fid);
0091     utils.helper.msg(utils.const.msg.PROC2, 'Counting lines: %d', maxLines);
0092   end
0093   fseek(fid, 0, 'bof');
0094 
0095   %%%%%%%%%%   Read data   %%%%%%%%%%
0096   readlines = min(50000, maxLines);
0097   nlines    = 0;
0098 
0099   %%% preallocate data array
0100   f_data = zeros(maxLines, read_col);
0101 
0102   %%% Look for the first line of data
0103   while ~feof(fid)
0104     f = deblank(fgetl(fid));
0105     if f(1) ~= comment_char
0106       break;
0107     end
0108   end
0109   %%% Scan it to find how many columns we have in the file
0110   C = textscan(f, scan_format, 1, ...
0111              'CommentStyle', comment_char, ...
0112              'CollectOutput', 1);  
0113   if any(isnan(C{:}))
0114     error('### Error in file format. Perhaps you specified more columns than the file contains?');
0115   end
0116   
0117   fseek(fid, 0, 'bof');
0118   %%% read file to end
0119   while ~feof(fid) && nlines < maxLines
0120     C = textscan(fid, scan_format, readlines, ...
0121              'CommentStyle', comment_char, ...
0122              'CollectOutput', 1);
0123     f_data(nlines+1:nlines+length(C{1}),:) = C{1};
0124     nlines = nlines + length(C{1});
0125 
0126     if isempty(C{1})
0127       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);
0128     end
0129 
0130     utils.helper.msg(utils.const.msg.PROC2, '  - read %09d lines of %09d', nlines, maxLines', maxLines);
0131   end
0132   fclose(fid);
0133 
0134   %%% get only the data we want
0135   if size(f_data,1) > nlines
0136     f_data = f_data(1:nlines, :);
0137   end
0138 
0139   %%%%%%%%%%   Create for each column pair the data object   %%%%%%%%%%
0140   if isempty(use_fs)
0141 
0142     %%%%%%%%%%   The numbers in columns must be straight   %%%%%%%%%%
0143     if mod(length(columns),2) ~= 0
0144       error('### the numbers in columns must be straight');
0145     end
0146 
0147     for lauf = 1:length(columns)/2
0148 
0149       data_x_axes = f_data(:, columns(lauf*2-1));
0150       data_y_axes = f_data(:, columns(lauf*2));
0151 
0152       % create data object corresponding to the parameter list
0153       ao_data = [];
0154       switch data_type
0155         case 'tsdata'
0156           ao_data = tsdata( data_x_axes, data_y_axes);
0157         case 'fsdata'
0158           ao_data = fsdata( data_x_axes, data_y_axes);
0159         case 'cdata'
0160           error ('### please code me up')
0161         case 'xydata'
0162           ao_data = xydata( data_x_axes, data_y_axes);
0163         otherwise
0164           error('### unknown data type ''%s''', data_type);
0165       end
0166       aa = ao(ao_data);
0167       aa.setName(sprintf('%s_%02d_%02d', file_name, orig_col(lauf*2-1), orig_col(lauf*2)), 'internal');
0168       % Add history
0169       pli = pli.pset('columns', [orig_col(lauf*2-1) orig_col(lauf*2)]);
0170       aa.addHistory(mi, pli, [], []);
0171 
0172       % Set the description fron the parameter list or from the file
0173       description_pl = find(pl, 'description');
0174       if ~isempty(description_pl)
0175         aa.description = description_pl;
0176       end
0177 
0178       a = [a aa];
0179 
0180     end
0181 
0182     %%%%%%%%%%   Create for each column AND fs a data object   %%%%%%%%%%
0183   else % isempty(use_fs)
0184 
0185     for lauf = 1:length(columns)
0186 
0187       data_y_axes = f_data(:, columns(lauf));
0188 
0189       % create data object corresponding to the parameter list
0190       ao_data = [];
0191       switch data_type
0192         case 'tsdata'
0193           ao_data = tsdata(data_y_axes, use_fs);
0194         case 'fsdata'
0195           ao_data = fsdata(data_y_axes, use_fs);
0196         case 'cdata'
0197           error ('### please code me up')
0198         case 'xydata'
0199           error ('### please code me up')
0200         otherwise
0201           error('### unknown data type ''%s''', data_type);
0202       end
0203       aa = ao(ao_data);
0204       aa.setName(sprintf('%s_%02d', file_name, orig_col(lauf)), 'internal');
0205       pli = pli.pset('columns', orig_col(lauf));
0206       aa.addHistory(mi, pli, [], []);
0207 
0208       % Set the description fron the parameter list or from the file
0209       description_pl = find(pl, 'description');
0210       if ~isempty(description_pl)
0211         aa.description = description_pl;
0212       end
0213 
0214       a = [a aa];
0215 
0216     end
0217 
0218   end
0219 
0220   %%%%%%%%%%   set fields of the AO from the parameter list   %%%%%%%%%%
0221   for ii = 1:length(a)
0222 
0223     for jj = 1:pli.nparams
0224 
0225       cmd = ['set' pli.params(jj).key(1) lower(pli.params(jj).key(2:end))];
0226 
0227       if ismethod(a(ii), cmd)
0228         if iscell(pli.params(jj).val) && length(pli.params(jj).val) == length(a)
0229           %%% Set the value in the cell
0230           feval(cmd, a(ii), pli.params(jj).val{ii}, 'internal');
0231           %%% Reset the value in the plistUset to this single value
0232           a(ii).hist.plistUsed.pset(pli.params(jj).key, pli.params(jj).val{ii});
0233         else
0234           %%% Set the value
0235           feval(cmd, a(ii), pli.params(jj).val, 'internal');
0236         end
0237       end
0238 
0239     end
0240 
0241   end % for-loop over all ao's
0242 
0243 end
0244 
0245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0246 %                               Local Functions                               %
0247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0248 
0249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0250 %
0251 % FUNCTION:    numlines
0252 %
0253 % SYNTAX:      count = numlines(fid);
0254 %
0255 % DESCRIPTION: Number of lines in an ASCII file
0256 %
0257 % HISTORY:     02-08-2002 Peter Acklam, CSSM post
0258 %                 Creation.
0259 %
0260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0261 
0262 function lines = numlines(fid)
0263 
0264   lines = 0;                           % number of lines in file
0265   nlchr = uint8(sprintf('\n'));        % newline chr as uint8
0266   bsize = 4 * 256 * 8192;              % block size to read
0267 
0268   while ~feof(fid)
0269     block = fread(fid, bsize, '*uint8');
0270     lines = lines + sum(block == nlchr);
0271   end
0272   if ~isempty(block)                   % in case file is empty
0273     lines = lines + double(block(end) ~= nlchr);
0274   end
0275 end
0276

Generated on Mon 25-Aug-2008 22:39:29 by m2html © 2003