Home > classes > @ao > fromFilenameAndPlist.m

fromFilenameAndPlist

PURPOSE ^

FROMFILENAMEANDPLIST Construct an ao from filename AND parameter list

SYNOPSIS ^

function a = fromFilenameAndPlist(ain, pli)

DESCRIPTION ^

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

 FUNCTION:    fromFilenameAndPlist

 DESCRIPTION: Construct an ao from filename AND parameter list

 CALL:        a = fromFilenameAndPlist(a, pl)

 PARAMETER:
              file_name: File name
              pl:        Parameter list object

 HISTORY:     07-05-2007 Hewitson
              Creation

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 % FROMFILENAMEANDPLIST Construct an ao from filename AND parameter list
0002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0003 %
0004 % FUNCTION:    fromFilenameAndPlist
0005 %
0006 % DESCRIPTION: Construct an ao from filename AND parameter list
0007 %
0008 % CALL:        a = fromFilenameAndPlist(a, pl)
0009 %
0010 % PARAMETER:
0011 %              file_name: File name
0012 %              pl:        Parameter list object
0013 %
0014 % HISTORY:     07-05-2007 Hewitson
0015 %              Creation
0016 %
0017 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0018 function a = fromFilenameAndPlist(ain, pli)
0019 
0020   utils.helper.msg(utils.const.msg.PROC1, 'constructing from filename and/or plist');
0021 
0022   VERSION = '$Id: fromFilenameAndPlist.m,v 1.12 2008/08/12 14:43:06 ingo Exp $';
0023 
0024   % get AO info
0025   mi = ao.getInfo('ao', 'From ASCII File');
0026 
0027   % Set the method version string in the minfo object
0028   mi.setMversion([VERSION '-->' mi.mversion]);
0029 
0030   % Get filename
0031   file_name = find(pli, 'filename');
0032 
0033   [pathstr, f_name, ext] = fileparts(file_name);
0034 
0035   %%%%%%%%%%   Get default parameter list   %%%%%%%%%%
0036   dpl = ao.getDefaultPlist('From ASCII File');
0037   pl = combine(pli, dpl);
0038 
0039   pl = pset(pl, 'filename', [f_name ext]);
0040   pl = pset(pl, 'filepath', pathstr);
0041 
0042   data_type    = find (pl, 'type');
0043   columns      = find (pl, 'columns');
0044   maxLines     = find(pl,  'maxlines');
0045   comment_char = find (pl, 'comment_char');
0046   use_fs       = find (pl, 'use_fs');
0047   xunits       = find (pl, 'xunits');
0048   yunits       = find (pl, 'yunits');
0049   t0           = find (pl, 't0');
0050   a            = [];
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   scan_format = '';
0060   for j=1:max(columns)
0061     if ismember(j, columns)
0062       scan_format = [scan_format '%f '];
0063     else
0064       scan_format = [scan_format '%*f '];
0065     end
0066   end
0067   scan_format = [deblank(scan_format) '%*[^\n]'];
0068 
0069   %%%%%%%%%%   Get/Count max number of lines   %%%%%%%%%%
0070   if isempty(maxLines) || maxLines < 0
0071     maxLines = 1e20;
0072   end
0073   if maxLines == 1e20
0074     maxLines = numlines(fid);
0075     utils.helper.msg(utils.const.msg.PROC2, 'Counting lines: %d', maxLines);
0076   end
0077   fseek(fid, 0, 'bof');
0078 
0079   %%%%%%%%%%   Read data   %%%%%%%%%%
0080   readlines = min(50000, maxLines);
0081   nlines    = 0;
0082 
0083   %%% preallocate data array
0084   f_data = zeros(maxLines, length(columns));
0085 
0086   %%% read file to end
0087   while ~feof(fid) && nlines < maxLines
0088     C = textscan(fid, scan_format, readlines, 'CommentStyle', comment_char, 'CollectOutput', 1);
0089     f_data(nlines+1:nlines+length(C{1}),:) = C{1};
0090     nlines = nlines + length(C{1});
0091 
0092     if isempty(C{1})
0093       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);
0094     end
0095 
0096     utils.helper.msg(utils.const.msg.PROC2, '  - read %09d lines of %09d', nlines, maxLines', maxLines);
0097   end
0098   fclose(fid);
0099 
0100   %%% get only the data we want
0101   if size(f_data,1) > nlines
0102     f_data = f_data(1:nlines, :);
0103   end
0104 
0105 
0106   %%%%%%%%%%   Create for each column pair the data object   %%%%%%%%%%
0107   if isempty(use_fs)
0108 
0109     %%%%%%%%%%   The numbers in columns must be straight   %%%%%%%%%%
0110     if mod(length(columns),2) ~= 0
0111       error('### the numbers in columns must be straight');
0112     end
0113 
0114     for lauf = 1:length(columns)/2
0115 
0116       data_x_axes = f_data(:, columns(lauf*2-1));
0117       data_y_axes = f_data(:, columns(lauf*2));
0118 
0119       % create data object corresponding to the parameter list
0120       ao_data = [];
0121       switch data_type
0122         case 'tsdata'
0123           ao_data = tsdata( data_x_axes, data_y_axes);
0124         case 'fsdata'
0125           ao_data = fsdata( data_x_axes, data_y_axes);
0126         case 'cdata'
0127           error ('### please code me up')
0128         case 'xydata'
0129           ao_data = xydata( data_x_axes, data_y_axes);
0130         otherwise
0131           error('### unknown data type ''%s''', data_type);
0132       end
0133       aa = ao(ao_data);
0134       aa.setName(sprintf('%s_%02d_%02d', file_name, columns(lauf*2-1), columns(lauf*2)), 'internal');
0135       % Add history
0136       aa.addHistory(mi, pli, [], []);
0137 
0138       % Set the description fron the parameter list or from the file
0139       description_pl = find(pl, 'description');
0140       if ~isempty(description_pl)
0141         aa.description = description_pl;
0142       end
0143 
0144       a = [a aa];
0145 
0146     end
0147 
0148     %%%%%%%%%%   Create for each column AND fs a data object   %%%%%%%%%%
0149   else % isempty(use_fs)
0150 
0151     for lauf = 1:length(columns)
0152 
0153       data_y_axes = f_data(:, columns(lauf));
0154 
0155       % create data object corresponding to the parameter list
0156       ao_data = [];
0157       switch data_type
0158         case 'tsdata'
0159           ao_data = tsdata(data_y_axes, use_fs);
0160         case 'fsdata'
0161           ao_data = fsdata(data_y_axes, use_fs);
0162         case 'cdata'
0163           error ('### please code me up')
0164         case 'xydata'
0165           error ('### please code me up')
0166         otherwise
0167           error('### unknown data type ''%s''', data_type);
0168       end
0169       aa = ao(ao_data);
0170       aa.setName(sprintf('%s_%02d', file_name, columns(lauf)), 'internal');
0171       aa.addHistory(mi, pli, [], []);
0172 
0173       % Set the description fron the parameter list or from the file
0174       description_pl = find(pl, 'description');
0175       if ~isempty(description_pl)
0176         aa.description = description_pl;
0177       end
0178 
0179       a = [a aa];
0180 
0181     end
0182 
0183   end
0184 
0185   %%%%%%%%%%   set fields of the AO from the parameter list   %%%%%%%%%%
0186   for ii = 1:length(a)
0187 
0188     for jj = 1:pli.nparams
0189 
0190       cmd = ['set' pli.params(jj).key(1) lower(pli.params(jj).key(2:end))];
0191 
0192       if ismethod(a(ii), cmd)
0193         if length(pli.params(jj).val) == length(a)
0194           feval(cmd, a(ii), pli.params(jj).val{ii});
0195         else
0196           feval(cmd, a(ii), pli.params(jj).val);
0197         end
0198       end
0199 
0200     end
0201 
0202   end % for-loop over all ao's
0203 
0204 end
0205 
0206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0207 %                               Local Functions                               %
0208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0209 
0210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0211 %
0212 % FUNCTION:    numlines
0213 %
0214 % SYNTAX:      count = numlines(fid);
0215 %
0216 % DESCRIPTION: Number of lines in an ASCII file
0217 %
0218 % HISTORY:     02-08-2002 Peter Acklam, CSSM post
0219 %                 Creation.
0220 %
0221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0222 
0223 function lines = numlines(fid)
0224 
0225   lines = 0;                           % number of lines in file
0226   nlchr = uint8(sprintf('\n'));        % newline chr as uint8
0227   bsize = 4 * 256 * 8192;              % block size to read
0228 
0229   while ~feof(fid)
0230     block = fread(fid, bsize, '*uint8');
0231     lines = lines + sum(block == nlchr);
0232   end
0233   if ~isempty(block)                   % in case file is empty
0234     lines = lines + double(block(end) ~= nlchr);
0235   end
0236 end
0237

Generated on Thu 14-Aug-2008 14:29:45 by m2html © 2003