% Construct a ltpda_ob from a file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    fromFile
%
% DESCRIPTION: Construct a ltpda_ob from a file
%
% CALL:        obj = obj.fromFile(filename)
%              obj = obj.fromFile(pl)
%
% VERSION:     $Id: fromFile.m,v 1.25 2011/10/05 09:50:55 ingo Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function objs = fromFile(obj, pli)
  
  VERSION = '$Id: fromFile.m,v 1.25 2011/10/05 09:50:55 ingo Exp $';
  
  % Which file type are we dealing with?
  if ischar(pli)
    pli = plist('filename', pli);
  end
  
  % get filename
  filename = find(pli, 'filename');
  
  % Get the correct parameter set
  [path, name, ext] = fileparts(filename);
  
  % Load a MAT file if the file extension doesn't exist
  if isempty(ext)
    ext = '.mat';
    filename = strcat(filename, ext);
    pli.pset('filename', filename);
  end
  
  % Some display information
  import utils.const.*
  utils.helper.msg(msg.PROC1, 'load file: %s%s', name, ext);
  
  switch ext
    
    case '.fil'
      objs = obj.fromLISO(pli);
      
    case '.mat'
      ii = obj.getInfo(class(obj), 'From MAT File');
      ii.setMversion([VERSION '-->' ii.mversion]);
      
      % Combine input and default plist
      pl = combine(pli, ii.plists);
      
      % Load MAT-File
      objs = load(filename);
      
      fn = fieldnames(objs);
      
      if (numel(fn) == 1) && (isstruct(objs.(fn{1})))
        % If the read object have only one entry and the this entry is a
        % struct then we assume that the struct is a LTPDA object.
        
        objs = objs.(fn{1});
        
        scl = utils.helper.classFromStruct(objs);
        if isempty(scl)
          if isfield(objs, 'class')
            scl = objs.class;
          else
            error('### The structure does not match any LTPDA object.');
          end
        end
        if ~strcmp(class(obj), scl)
          error('### The structure does not match the chosen LTPDA object constructor. It seems to be a [%s] object.', scl)
        end
        fcn_name   = [class(obj) '.update_struct'];
        if ~isempty(objs(1).hist) && ~isempty(objs(1).hist.plistUsed)
          struct_ver = sscanf(objs(1).hist.plistUsed.creator.ltpda_version, '%s.%s.%s');
        else
          struct_ver = '1.0';
        end
        objs = feval(fcn_name, objs, struct_ver);
        objs = feval(class(obj), objs);
        
      elseif (numel(fn) == 1) && (isa(objs.(fn{1}), 'ltpda_obj'))
        % If the read object have only one entry and this entry is a LTPDA
        % object then return this LTPDA object.
        objs = objs.(fn{1});
        
      else
        objs = obj.fromDataInMAT(objs, pl);
      end
      
    case '.xml'
      ii = obj.getInfo(class(obj), 'From XML File');
      ii.setMversion([VERSION '-->' ii.mversion]);
      
      root_node = xmlread(filename);
      objs = utils.xml.xmlread(root_node, class(obj));
      
    otherwise
      % we load an ascii file
      
      if pli.isparam('complex_type')
        objs = obj.fromComplexDatafile(pli);
      else
        objs = obj.fromDatafile(pli);
      end
      
  end
  
end

