function self = netcdf_native(varargin)
% file @netcdf_native/netcdf_native
%
% $Id$

arg = nargin;

if arg == 1 && isempty(varargin{1})
  arg = 0;
end

theFile = ''; theDynaload = 'memory'; theMode = 'MEMORY';

switch arg
  
  % with not arg, read netcdf file with uigetfile
  % ---------------------------------------------
  case 0
    [theFileName, thePathName] = uigetfile(...
      {'*.nc;*.cdf','NetCDF (*.nc,*.cdf)'}, 'Select file');
    if any(theFileName)
      theFile = fullfile(thePathName, theFileName);
      theMode = 'NC_NOWRITE';
      theDynaload = 'memory';
    else
      theFile = '';
    end
    
    % with one arg, read given netcdf filename
    % ----------------------------------------
  case 1
    if( strcmp(varargin{1}, 'memory'))
      theDynaload = 'memory';
      theFile = 'memory';
      theMode = 'MEMORY';
      
    elseif( isa(varargin{1}, 'char'))
      theFile = varargin{1};
      if isempty(exist(theFile, 'file'))
        error('netcdf: %s file don''t exist.\n', theFile );
      end
      [thePathName, theFileName, theFileExt] = fileparts(varargin{1});
      
      switch theFileExt
        case {'.nc', 'cdf'}
          theMode = 'NC_NOWRITE';
          % a revoir ...
          theDynaload = 'memory';
        case {'.csv', '.xls'}
          theDynaload = varargin{1};
          theMode = 'DYNALOAD';
        otherwise
          error('Wrong file type');
      end
      
    else
      error('Wrong input argument');
    end
    
    % work on given netcdf filename with mode and dynaload dexcriptor
    % file
    % ---------------------------------------------------------------
  case 2
    
    % is the mode numeric.
    if ischar(varargin{2})
      %  convert it in numeric value
      switch (varargin{2})
        case { 'NC_WRITE', 'NC_CLOBBER', 'NC_NOCLOBBER', 'NC_SHARE' };
          theMode = varargin{2};
        otherwise
          error('Wrong mode to create netcdf file: %s', varargin{2});
      end
    else
      % a modifier sur mode est numeric
      theMode = varargin{2};
    end
    
  otherwise
    error('Wrong number of input arguments');
end

d = dynaload( theDynaload );

self.file      = theFile;
%self.dynaload = theDynaload;
self.mode      = theMode;
self.nc_id     = 0;
self.autonan   = logical(1);
self.autoscale = logical(0);

% constants for netcdf data types
% -------------------------------
self.NC_BYTE             = netcdf.getConstant('NC_BYTE');    % 1
self.NC_CHAR             = netcdf.getConstant('NC_CHAR');    % 2
self.NC_SHORT            = netcdf.getConstant('NC_SHORT');   % 3
self.NC_INT              = netcdf.getConstant('NC_INT');     % 4
self.NC_LONG             = netcdf.getConstant('NC_LONG');    % 4
self.NC_FLOAT            = netcdf.getConstant('NC_FLOAT');   % 5
self.NC_DOUBLE           = netcdf.getConstant('NC_DOUBLE');  % 6

self.NC_MEMORY           = -1;

% constants for netcdf.open()
% ---------------------------
%self.NC_NOWRITE:	Read-only access
self.NC_NOWRITE          = netcdf.getConstant('NC_NOWRITE');   % 0

%self.NC_WRITE:      	Read-write access
self.NC_WRITE            = netcdf.getConstant('NC_WRITE');     % 1

% constants for netcdf.create()
% -----------------------------
self.NC_CLOBBER          = netcdf.getConstant('NC_CLOBBER');   % 0

%self.NC_NOCLOBBER:    Prevent overwriting of existing file with the same name.
self.NC_NOCLOBBER        = netcdf.getConstant('NC_NOCLOBBER'); % 4

%self.NC_SHARE:	       Allow synchronous file updates.
self.NC_SHARE            = netcdf.getConstant('NC_SHARE');     % 2048

%self.NC_64BIT_OFFSET: Allow easier creation of files and variables which
%                  are larger than two gigabytes.
self.NC_64BIT_OFFSET     = netcdf.getConstant('NC_64BIT_OFFSET');  % 512

self.NC_ERR             = netcdf.getConstant('NC2_ERR');      % -1
self.NC_EBADDIM          = netcdf.getConstant('NC_EBADDIM');
self.NC_EBADID           = netcdf.getConstant('NC_EBADID');
self.NC_EBADNAME         = netcdf.getConstant('NC_EBADNAME');
self.NC_EBADTYPE         = netcdf.getConstant('NC_EBADTYPE');
self.NC_ECHAR            = netcdf.getConstant('NC_ECHAR');
self.NC_EDIMSIZE         = netcdf.getConstant('NC_EDIMSIZE');
self.NC_EEDGE            = netcdf.getConstant('NC_EEDGE');
self.NC_EEXIST           = netcdf.getConstant('NC_EEXIST');
self.NC_EGLOBAL          = netcdf.getConstant('NC_EGLOBAL');
self.NC_EINDEFINE        = netcdf.getConstant('NC_EINDEFINE');
self.NC_EINVAL           = netcdf.getConstant('NC_EINVAL');
self.NC_EINVALCOORDS     = netcdf.getConstant('NC_EINVALCOORDS');
self.NC_EMAXATTS         = netcdf.getConstant('NC_EMAXATTS');
self.NC_EMAXDIMS         = netcdf.getConstant('NC_EMAXDIMS');
self.NC_EMAXNAME         = netcdf.getConstant('NC_EMAXNAME');
self.NC_EMAXVARS         = netcdf.getConstant('NC_EMAXVARS');
self.NC_ENAMEINUSE       = netcdf.getConstant('NC_ENAMEINUSE');
self.NC_ENFILE           = netcdf.getConstant('NC_ENFILE');
self.NC_ENOMEM           = netcdf.getConstant('NC_ENOMEM');
self.NC_ENORECVARS       = netcdf.getConstant('NC_ENORECVARS');
self.NC_ENOTATT          = netcdf.getConstant('NC_ENOTATT');
self.NC_ENOTINDEFINE     = netcdf.getConstant('NC_ENOTINDEFINE');
self.NC_ENOTNC           = netcdf.getConstant('NC_ENOTNC');
self.NC_ENOTVAR          = netcdf.getConstant('NC_ENOTVAR');
self.NC_EPERM            = netcdf.getConstant('NC_EPERM');
self.NC_ERANGE           = netcdf.getConstant('NC_ERANGE');
self.NC_ESTRIDE          = netcdf.getConstant('NC_ESTRIDE');
self.NC_ESTS             = netcdf.getConstant('NC_ESTS');
self.NC_ETRUNC           = netcdf.getConstant('NC_ETRUNC');
self.NC_EUNLIMIT         = netcdf.getConstant('NC_EUNLIMIT');
self.NC_EUNLIMPOS        = netcdf.getConstant('NC_EUNLIMPOS');
self.NC_EVARSIZE         = netcdf.getConstant('NC_EVARSIZE');
self.NC_FATAL            = netcdf.getConstant('NC_FATAL');       % 1
self.NC_FILL             = netcdf.getConstant('NC_FILL');        % 0
self.NC_FILL_BYTE        = netcdf.getConstant('NC_FILL_BYTE');   % -127
self.NC_FILL_CHAR        = netcdf.getConstant('NC_FILL_CHAR');   % 0
self.NC_FILL_DOUBLE      = netcdf.getConstant('NC_FILL_DOUBLE'); % 9.9692e+036
self.NC_FILL_FLOAT       = netcdf.getConstant('NC_FILL_FLOAT');  % 9.9692e+036
self.NC_FILL_INT         = netcdf.getConstant('NC_FILL_INT');    % -2.1475e+009
self.NC_FILL_SHORT       = netcdf.getConstant('NC_FILL_SHORT');  % -32767
self.NC_FORMAT_64BIT     = netcdf.getConstant('NC_FORMAT_64BIT');   % 2
self.NC_FORMAT_CLASSIC   = netcdf.getConstant('NC_FORMAT_CLASSIC'); % 1
self.NC_GLOBAL           = netcdf.getConstant('NC_GLOBAL');       % -1
self.NC_LOCK             = netcdf.getConstant('NC_LOCK');         % 1024
self.NC_MAX_ATTRS        = netcdf.getConstant('NC_MAX_ATTRS');    % 8192
self.NC_MAX_DIMS         = netcdf.getConstant('NC_MAX_DIMS');     % 1024
self.NC_MAX_NAME         = netcdf.getConstant('NC_MAX_NAME');     % 256
self.NC_MAX_VARS         = netcdf.getConstant('NC_MAX_VARS');     % 8192
self.NC_MAX_VAR_DIMS     = netcdf.getConstant('NC_MAX_VAR_DIMS'); % 1024
self.NC_NAT              = netcdf.getConstant('NC_NAT');          % 0
self.NC_NOERR            = netcdf.getConstant('NC_NOERR');        % 0
self.NC_NOFILL           = netcdf.getConstant('NC_NOFILL');       % 256
self.NC_SIZEHINT_DEFAULT = netcdf.getConstant('NC_SIZEHINT_DEFAULT');  % 0
self.NC_UNLIMITED        = netcdf.getConstant('NC_UNLIMITED');    % 0
self.NC_VERBOSE          = netcdf.getConstant('NC_VERBOSE');      % 2

% bless class
% -----------
self = class(self, 'netcdf_native', d);

% call private function following mode state
% a revoir !!!!!!!!!!!!!!!
% ------------------------------------------
switch self.mode
  case 'NC_NOWRITE'     % NOWRITE mode
    [self] = read(self, true);
  case 'NC_WRITE'     % READ/WRITE mode
    self = put(self,'DIMENSIONS', hashtable);
    self = put(self,'VARIABLES',  hashtable);
    self = put(self,'ATTRIBUTES', hashtable);
  case 'NC_NOCLOBBER'     % NOCLOBBER mode
  case 'NC_CLOBBER'     % NOCLOBBER mode
    self = put(self,'DIMENSIONS', hashtable);
    self = put(self,'VARIABLES',  hashtable);
    self = put(self,'ATTRIBUTES', hashtable);
    %         case self.NC_SHARE  % SHARE mode
    %           % not implemeted
  case 'NC_64BIT_OFFSET'
    %           %
  case 'DYNALOAD'
    %           %
  case 'MEMORY'
    self = put(self,'DIMENSIONS', hashtable);
    self = put(self,'VARIABLES',  hashtable);
    self = put(self,'ATTRIBUTES', hashtable);
  otherwise
    error('Wrong mode argument');
end




