function ncv = tsg_nc( varargin )

% tsg_nc/tsg_nc -- Constructor for "tsg_nc" class.
% tsg_nc(varargin) constructs a 'tsg_nc' object from 
% supplied csv 'filename'.
%
% Usage:  
%          nc = tsg_nc( file )
%
% Example:
%          ncv = tsg_nc('tsg_ncvar.csv')
%
% Input
% -------
% filename ........... csv data filename
%
% Output
% --------
% nc       ........... instance of tsg_nc object
%
% See also: methods('tsg_nc').
% 
% $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',';');

% generate one line format
% ------------------------
format = repmat('%s', 1, numel(members{1}));

% read end of file
% ----------------
values = textscan(fid, format, 'delimiter',';');

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

% loop
% ----
for i=1:ncv.size
  
  % get id (first character indentifying begining & start of line (#)
  % -----------------------------------------------------------------
  id   = values{1}{i};
  endl = values{ncv.members}{i};
  
  % check if line begin and end with '#' (we add the 5 header lines)
  % ----------------------------------------------------------------_
  if ~(strcmp(id, '#') & strcmp(endl, '#'))
    error(['mismatch field number in ' file ', line: ' int2str(i+5) ...
           ' ... Check your file']);
  end
  
  % get key
  % -------
  key = values{2}{i};
  
  % loop over other members find in line
  % ------------------------------------
  for j=3:ncv.members-1
    member = genvarname(members{1}{j});
    type   = genvarname(types{1}{j});
    value  = values{j}{i};
  
    % dynamically populate structure 'theStruct' 
    % ------------------------------------------
    if isempty(value)
      theStruct.(member) = [];
    else
      switch type
        
        case 'char'
          theStruct.(member) = value;
          
        % eval needed to construct matlab cell expression
        % -----------------------------------------------
        case 'cell'
          theStruct.(member) = eval(['{' value '}']);
          
        case {'integer','double'}
          theStruct.(member) = str2double(value);
          
        otherwise
          theStruct.(member) = value; 
          
      end
    end
  end
    
  % populate the hashtable
  % ----------------------
  data = put(data, key, theStruct);
  
  % initialize internal structure for next loop
  % -------------------------------------------
  theStruct = [];
  
end

% remove id, first member (key) and endl in structure 
% ---------------------------------------------------
ncv.members  = ncv.members - 3;

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

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