Skip to content
Snippets Groups Projects
dynaload.m 4.59 KiB
Newer Older
function self = dynaload(varargin)
%
% file @dynaload/dynaload
%
% Class used to load complex object descriptor from Excel xls or ASCII csv
% files
% Usually Excel file is used during development under Windows, and csv file is
% create and use in production.
%
% >> nc = dynaload('tsgqc_netcdf.xls');
% >> nc
% dynaload
%
% 	file:	..\MATLAB\toolbox\@dynaload\tsgqc_netcdf.csv
%
% 	DIMENSIONS               	 8 [hashtable]
% 	VARIABLES                	60 [hashtable]
% 	ATTRIBUTES               	31 [hashtable]
% 	QUALITY                  	10 [hashtable]
%
% >> s = nc.VARIABLES.SSTP
%
% s =
%            code__: 'SSTP'
%       dimension__: {'DAYD'}
%            type__: 'float'
%         long_name: 'SEA SURFACE TEMPERATURE'
%     standard_name: 'surface temperature'
%             units: 'degrees Celsius'
%       conventions: ''
%         valid_min: -1.5000
%         valid_max: 38
%            format: '%6.3lf'
%        FillValue_: 99999
%         epic_code: []
%              axis: ''
%        resolution: 1.0000e-003
%           comment: [1x99 char]
%     missing_value: []
%
% % get value :
% >> nc.VARIABLES.SSTP.long_name
% ans = SEA SURFACE TEMPERATURE
%
% % change value :
% >> nc.VARIABLES.SSTP.long_name = 'NEW SEA SURFACE TEMPERATURE'
%
% % convert Excel file to csv :
% >> write(nc, '+/@dynaload/tsgqc_netcdf.csv');
%
% >> type +/@dynaload/tsgqc_netcdf.csv
%
% tsgqc_netcdf.csv
%
% $DIMENSIONS$
% #;code;value;#
% #;char;integer;#
% #;PROFILE;120;#
% ...
% %
% $VARIABLES$
% #;code;dimension;nctype;long_name;standard_name;units;conventions;valid_min;valid_max;format;FillValue_;epic_code;axis;resolution;comment;missing_value;#
% #;char;cell;char;char;char;char;char;double;double;char;double;double;char;double;char;double;#
% #;REFERENCE_DATE_TIME;'STRING14';char;REFERENCE DATE TIME FOR JULIAN DAYS;reference date time;;yyyymmddhhmmss;;;;;;;;Reference date for julian days origin;;#
% ...
%
% An instance of dynaload can be create in memory:
%
% m = dynaload('memory');
% m.DIMENSIONS = hashtable;
% m.VARIABLES  = hashtable;
% m.DIMENSIONS.STRING4 = 4;
% m.VARIABLES.TEMP.data = [20.0 21.2 23.5];
% m.VARIABLES.TEMP.nctype = 'float';
% m.VARIABLES.TEMP.dimension = {'DAYD'};
% m.VARIABLES.TEMP.long_name ='Temperature';
% m.VARIABLES.TEMP.FillValue = '99999';
% ...
%

%% COPYRIGHT & LICENSE
%  Copyright 2009 - IRD US191, all rights reserved.
%
%  This file is part of  Matlab package.
%
%    tsgqc package is free software; you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation; either version 2 of the License, or
%    (at your option) any later version.
%
%    tsgqc package is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with this program; if not, write to the Free Software
%    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

%% Class definition
% -------------------

% test constructor argument
% -------------------------
switch nargin
  case 0  % can't create default object
    [file, path, filterIndex] = uigetfile(...
      {'*.csv','Ascii-file (*.csv)';...
      '*.xls','Excel-file (*.xls)'}, 'Select file');
    if ~any(file)
      self.file = 'memory';
      filterIndex = -1;
    else
      self.file = fullfile(path, file);
  case 1
    if strcmp(varargin{1}, 'memory')
      self.file = 'memory';
      filterIndex = -1;
    elseif( isa(varargin{1}, 'char'))
      file = which(varargin{1});
      if isempty( file)
        file = varargin{1};
      end
      [path, name, ext] = fileparts(file);
      switch ext
        case '.csv'
          filterIndex = 1;
        case '.xls'
          filterIndex = 2;
        otherwise
          error('Wrong file type');
      end
      self.file = file;
    else
      error('Wrong input argument');
    end
  otherwise
    error('Wrong number of input arguments');
end

% init class properties
% ----------------------
self.line               = 0;
self.fid                = 0;

% bless class
% -----------
self = class(self, 'dynaload', hashtable);

% call read_xxx_file functions
% ----------------------------
switch filterIndex
  case 1
    [self] = read_csv_file(self);
  case 2
    [self] = read_xls_file(self);
  otherwise
end