function netcdf_info_gui(varargin)
% netcdf_info_gui
% Display netCDF information of selected file and variables
%08/2011 - Ludovic Bariteau -
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Initialization tasks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% screenSize  = get(0,'ScreenSize');
% set(0,'Units','pixels');
% screenCx = screenSize(3)/2;
% screenCy = screenSize(4)/2;
% guiW = 800;
% guiH = 530;
% guiX = screenCx-guiW/2;
% guiY = screenCy-guiH/2;
% f = figure('Visible','on','Name','netcdf_info','MenuBar','none','NumberTitle','off','units','pixel', 'Position',[guiX,guiY,guiW,guiH]);

GAttList={''};
VarList={'no var'};
VarAttList={''};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Construct the components
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
FileInfo = uicontrol('Style','pushbutton','String','Select the netCDF file to check ...','Units','normalized','Position',[0.025,0.85,0.85,0.08],...
    'CallBack', @FileInfoCallBack, 'HorizontalAlignment','left','tag','FileInfo','fontsize',14);
FileLoad = uicontrol('Style','pushbutton','String','Read','Units','normalized','Position',[0.9,0.85,0.075,0.08],...
    'CallBack', @FileLoadCallBack, 'HorizontalAlignment','left','tag','Load','fontsize',14);
Fileph = uipanel('Title','File','Units','pixels','Units','normalized','Position',[0.015,0.81,0.97,0.18],'fontsize',14);

GAttBox = uicontrol('Style','ListBox','String',GAttList,'Max',2,'Min',0,'Value',[],...
    'Units','normalized','Position',[0.025,0.39,0.95,0.35],'tag','GAttBox','fontsize',13);
GAttTitle={'General Attributes'};
GAttph = uipanel('Title',GAttTitle,'Units','pixels','Units','normalized','Position',[0.015,0.34,0.97,0.45],'tag','GAttpanel','fontsize',14);

VarBox = uicontrol('Style','popupmenu','String',VarList,'Units','normalized','Position',[0.04,0.10,0.19,0.11],'CallBack', @VarBoxCallBack,'tag','VarBox','fontsize',14);
VarAttBox = uicontrol('Style','ListBox','String',VarAttList,'Max',2,'Min',0,'Value',[],...
    'Units','normalized','Position',[0.275,0.038,0.7,0.25],'tag','VarAttBox','fontsize',13);
VarTitle={'Variable Attributes'};
VarAttph = uipanel('Title',VarTitle','Units','normalized','Position',[0.015,0.018,0.97,0.31],'tag','VarAttpanel','fontsize',14);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Callbacks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    function FileInfoCallBack(h, eventdata)
        [FileName,PathName] = uigetfile('*.nc','Select the netCDF file');
        FileInfo = fullfile(PathName, FileName);
        obj = findobj(gcf,'tag','FileInfo');
        set(obj,'string',FileInfo);
    end

    function FileLoadCallBack(h, eventdata)
        try
            FID = netcdf.open(FileInfo,'NC_NOWRITE');
            if FID>0
                FileLoad=FID;
                [numdims, numvars, numglobalatts, unlimdimID] = netcdf.inq(FID);
                obj = findobj(gcf,'tag','VarBox');            
                set(obj,'string',num2str(numvars));
                
                GAttList=cell(1,numglobalatts+numdims);
                for ii=0:numglobalatts-1
                    gattname = netcdf.inqAttName(FID,netcdf.getConstant('NC_GLOBAL'),ii);
                    gattval = netcdf.getAtt(FID,netcdf.getConstant('NC_GLOBAL'),gattname);                   
                    GAttList{ii+1}=[gattname ':  ' num2str(gattval)]; 
                end
                for ii=0:numdims-1
                    [dimname, dimlen] = netcdf.inqDim(FID,ii);
                    GAttList{numglobalatts+ii+1}=[dimname ':  ' num2str(dimlen)]; 
                end    
                obj = findobj(gcf,'tag','GAttBox');
                set(obj,'string',GAttList);
                
                
                VarList=cell(1,numvars);
                for ii = 0:numvars-1
                    [varname, xtype, dimids, numatts] = netcdf.inqVar(FID,ii);
                    VarList{ii+1}=[num2str(ii) '. ' varname]; 
                end
                obj = findobj(gcf,'tag','VarBox');
                set(obj,'string',VarList);
                
                
            else
                errordlg('Unable to read the file','File Error');
            end    
        catch
            errordlg('No file loaded yet','File Error');
        end         
    end

    function VarBoxCallBack(h, eventdata)
        obj = findobj(gcf,'tag','VarBox');
        VarID = get(obj, 'Value')-1;
        [varname, xtype, dimids, numatts] = netcdf.inqVar(FileLoad,VarID);
        
        VarAttList=cell(1,numatts);
        VarAttList{1}=['---------- < ' varname ' (ID# ' num2str(VarID) ') > ----------'];
        for ii = 0:numatts - 1
            attname1 = netcdf.inqAttName(FileLoad,VarID,ii);
            attname2 = netcdf.getAtt(FileLoad,VarID,attname1);    
            VarAttList{ii+2}=[attname1 ':  ' num2str(attname2)];   
        end       
        obj = findobj(gcf,'tag','VarAttBox');
        set(obj,'string',VarAttList);
    end

end