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


classdef FILE_INFO
    properties (SetAccess = private, GetAccess = public)
        fd;
    end
    
    methods
        
        function self = FILE_INFO ( filename)
            % Create the constructor
            self.fd = H5F.open(filename);
        end
        %------------------------------------
        
        function delete(self)
            % Define the destructor
            disp('Closing file access...');
            H5F.close(self.fd)
        end
        %------------------------------------
        
        function displayFileInfo(obj)
            % Display info about the h5 file
            h5disp(obj.getName())
        end
        %------------------------------------
        
        function  list = listGroups(obj)
            % List group and subgroup names
            info  =  h5info(H5F.get_name(obj.fd));
            list=[];
            for i=1:size (info.Groups,1)
                list = [ list cellstr( info.Groups(i,1).Name)];
                if size (info.Groups(i,1).Groups,1) ~= 0
                    for j=1:size (info.Groups(i,1).Groups,1)
                        list= [ list cellstr(info.Groups(i,1).Groups(j,1).Name)];
                    end
                end
            end
        end
        %------------------------------------
        
        function gr = getGroup(obj,group_name)
            % Return a group object
            gr = GROUP_INFO(obj.fd, group_name);
        end
        %------------------------------------
        
        function  attr = getAttr(obj, attr_name)
            % Get file attribute(s)
            try
                attr_id = H5A.open(obj.fd, attr_name);
            catch err
                error('The attribute named "%s" does not exist',attr_name);
            end
            attr = H5A.read(attr_id)';
            H5A.close(attr_id);
        end
        %------------------------------------
        
        function size = getH5Size(obj)
            % Get the size of the h5 file
            size = H5F.get_filesize(obj.fd);
        end
        %------------------------------------
        
        function fname = getName(obj)
            % Return h5 file name
            fname = H5F.get_name(obj.fd);
        end
    end
    %------------------------------------    
end

