function ncv = tsg_nc( varargin )

% tsg_nc/tsg_nr -- Constructor for "tsg_nc" class.
% tsg_nc(varargin) constructs a 'tsg_nc' object from 
% supplied csv 'filename'.
%
% Usage:  nca = tsg_ncvar( file )
%
% Input
% -------
% filename ........... csv data filename
%
% Output
% --------
% ncv      ........... instance of tsg_ncvar object
%
% See also: methods('tsg_ncvar').
% 
% $Id$

% check varargin
% --------------
switch nargin
  case 0  % create default object
    error('csv filename not supplied');
  case 1
    if( isa(varargin{1}, 'char'))
      file = varargin{1};      
    else
      error('Wrong input argument');
    end
  otherwise
    error('Wrong number of input arguments');
end

% check csv file validity 
% -----------------------
fid = fopen( file, 'r' );
if fid == -1
  error(['The specified data file ''%s'' does not exist ...\n' ...
         'or is not in the directory which is on the MATLAB search path'],...
         file);   
end

% create hashtable
% ----------------
data = hashtable;

% read csv file
% -------------
% read 2 first lines
header   = textscan(fid,'%s',2,'delimiter','\n','headerlines', 3 );
% extract each members from first header line
members  = textscan(header{1}{1},'%s','delimiter',';');
% extract data type from second header line
types    = textscan(header{1}{2},'%s','delimiter',';');
% read end of file
values   = textscan(fid,'%s','delimiter',';');

% populate private structure ncv
% ------------------------------
ncv.file    = which( file );
ncv.size    = numel(values{1}) / numel(members{1});
ncv.member  = numel(members{1}); 

% check nvc.size (number of line) is an integer
% ---------------------------------------------
if mod(numel(values{1}),numel(members{1}))
  warning(['Incorrect number parameters in file: ' file]);
end

% loop
% ----
for i=1:ncv.size
  % may be give type to key ?
  line   = str2double(values{1}{1 + ncv.member * (i -1)});
  key    = values{1}{2 + ncv.member * (i -1)};
  
  for j=3:ncv.member
    member = genvarname(members{1}{j});
    type   = genvarname(types{1}{j});
    value  = values{1}{j + ncv.member * (i -1)};
  
    % dynamically populate structure 'theStruct' 
    % ------------------------------------------
    if isempty(values{1}{j + ncv.member * (i -1)})
      theStruct.(member) = [];
    else
      switch type
        case 'char'
          theStruct.(member) = value;
        case 'cell'
          theStruct.(member) = eval(['{' value '}']);
        case {'integer','double'}
          theStruct.(member) = str2double(value);
        otherwise
          theStruct.(member) = value; 
      end
    end
  end
  
  % check if i (number of line) should be equal to line
    % -------------------------------------------------------
    if line ~= i
      error(['line number mismatch in ' file ', line: ' int2str(i-1) ...
        '... Check your file']);
    end
    
  % populate the hashtable
  % ----------------------
  data = put(data, key, theStruct);
  
  % initialize internal structure for next loop
  % -------------------------------------------
  theStruct = [];
  
end

% remove line and first member (key)in structure 
% ----------------------------------------------
ncv.member  = ncv.member-2;

% bless tsg_nc class and inherite from hashtable data
% ---------------------------------------------------
ncv = class(ncv, 'tsg_nc', data );

% close file
% ----------
fclose(fid);
