%-------------------------------------------------------------------------%
% GROUP_INFO.m
%-------------------------------------------------------------------------%
% Auteur: Aminata NDIAYE, CGTD ICARE 2013
%-------------------------------------------------------------------------%
% Version: 0
%-------------------------------------------------------------------------%


classdef GROUP_INFO
    properties (SetAccess = private, GetAccess = public)
        fd;
        fname;
        gid;
        gname;
    end
    
    methods
        function self = GROUP_INFO ( var, groupname)
            % Create the constructor
            if isnumeric(var) || isa(var, 'H5ML.id')
                if  H5I.is_valid(var) == 0 %
                    error(' The Id %d "is" not valid', var);
                end
                self.fd = var;
                
                self.fname =H5F.get_name(self.fd);
                
                self.gid = H5G.open(self.fd, groupname);
                
                self.gname = groupname;
                
            else
                
                self.fd = H5F.open(var);
                
                self.fname = var;
                
                self.gid = H5G.open(self.fd, groupname) ;
                
                self.gname = groupname;
            end
        end
        %------------------------------------
        
        function delete(self)
            % Destructor
            disp('Closing group access...');
            H5G.close(self.gid);
        end
        %------------------------------------
        
        function displayGroupInfo(obj)
            % Display info about the group
            h5disp(obj.fname, obj.gname)
        end
        %------------------------------------
        
        function [l_ds, l_sg] =  getList(obj)
            % List ds and subgroup names 
            l_ds = [];
            l_sg = [];
            info = h5info(obj.fname, obj.gname);
            
            sprintf('Number of dataset %d', size(info.Datasets,1))
            for i=1:size(info.Datasets,1)
                l_ds = [ l_ds cellstr( info.Datasets(i,1).Name)];
            end
            sprintf('Number of sub groups %d', size(info.Groups,1))
            for j=1:size(info.Groups,1)
                l_sg = [ l_sg cellstr( info.Groups(j,1).Name)];
            end                        
        end
         %------------------------------------
         
        function data = getSdsBinaryData (obj, dataset_name, varargin)
            % Get binary data: varagin{1}=start, varagin{2}=count,
            % varagin{3}=stride
            if nargin == 2
                data = h5read(obj.fname, dataset_name);
            elseif nargin == 4
                data = h5read(obj.fname, dataset_name, varargin{1}, varargin{2});
            elseif nargin == 5
                data = h5read(obj.fname, dataset_name, varargin{1}, varargin{2}, varargin{3});
            end
        end
        %------------------------------------
        
        function units = getUnits(obj, dataset_name)
            %  Get units
            try
                units = h5readatt(obj.fname, dataset_name, 'units');
            catch err
                error('The attribute named "units" does not existe for "%s"', dataset_name);
            end
        end
        %------------------------------------
        
        function fill = getFillvalue(obj, dataset_name)
            % Get fillvalue
            try
                fill =  str2double(h5readatt(obj.fname, dataset_name, '_FillValue'));
            catch err
                error('The attribute named "fillvalue" does not exist for "%s"',dataset_name);
            end
        end
        %------------------------------------
        
        function offset= getOffset(obj, dataset_name)
            % Get offset
            try
                offset = str2double(h5readatt(obj.fname,dataset_name, 'add_offset'));
            catch err
                error('The attribute named "add_offset" does not exist for "%s"',dataset_name);
            end
        end
        
        %------------------------------------
        
        function scale_factor = getScaleFactor(obj, dataset_name)
            % Get scale factor
            try
                scale_factor = str2double(h5readatt(obj.fname, dataset_name, 'scale_factor'));
            catch err
                error('The attribute named "scale_factor" does not exist for "%s"',dataset_name);
            end
        end
        %------------------------------------
        
        function scaled_data = getScaledData (obj, dataset_name, varargin)
            % Get scaled data: varagin{1}=start, varagin{2}=count,
            % varagin{3}=stride
            data =  obj.getSdsBinaryData (dataset_name,varargin{:});
            scale_factor =  obj.getScaleFactor(dataset_name );
            offset =  obj.getOffset(dataset_name );
            scaled_data =double(data-offset)*scale_factor;
        end
        %------------------------------------
        
        function  infos = getGroupAttribute(obj, attr_name)
            % Get group attribute
            infos = h5readatt(obj.fname, obj.gname, attr_name);
        end
        %------------------------------------
        
        function  infos = getDsAttribute(obj, ds_name, attr_name)
            % Get dataset attribute
            infos = h5readatt(obj.fname, ds_name, attr_name);
        end
    end
    %------------------------------------    
end
