function result = save(self)
%
% save tsg_nc objet in csv file with header description
%
% $Id$

% return filename
% ---------------

global VERSION;

% Open the file
% -------------
fid = fopen( self.file, 'wt' );
  
%if fid ~= -1
%fid = 1;

% get header information from object
% ----------------------------------
key   = keys(self.header);
type  = values(self.header);
nb    = numel(key);

% Write the date and time when the file is saved
% ----------------------------------------------
% TODOS: a changer, le nom tsg_platform ne doit pas etre en dur !!!!!
%
fprintf( fid, '%% tsg_platform.csv:  %d members & %d lines with data; VERSION = %6.4f\n',...
   self.members, self.size, VERSION) ;

fprintf( fid, '%%\n%%\n');
    
% write header
% ------------
for i=1:nb-1;
  fprintf( fid, '%s;', key{i});
end
fprintf( fid, '%s\n', key{nb});

for i=1:nb-1;
  fprintf( fid, '%s;', type{i});
end
fprintf( fid, '%s\n', type{i});

% get header information from object
% ----------------------------------
data = keys(self.hashtable);

% write data
% ----------

for i=1:self.size;
  str = [];
  for j=1:nb;
    switch type{j}
      case 'char'
        format = '%s';
      case 'byte'
        format = '%u';  
      case 'integer'
        format = '%d';
      case 'float'
        format = '%f';
      case 'double'
        format = '%lf';
      otherwise
        format = '%s';
    end
    
    switch j
      case 1 
        str = [str, '#;'];
      case nb
        str = [str, '#'];
      case 2
         str = [str,sprintf('%s;', data{i})];
      otherwise
        value = get(self, data{i}, key{j});
        if isempty(value)
          str = [str, ';'];
        else
          str = [str, sprintf(strcat(format, ';'), value)];
        end
    end 
  end
  fprintf( fid, '%s\n', str);
end

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