function values = nc_attget(ncfile, varname, attribute_name )
% NC_ATTGET: Get the values of a NetCDF attribute.
%
% USAGE:  att_value = nc_attget(ncfile, varname, attribute_name);
%
% PARAMETERS:
% Input:
%   ncfile:  
%       name of netcdf file in question
%   varname:  
%       name of variable in question.  Use nc_global to retrieve a 
%       global attribute.
%   attribute_name:  
%       name of attribute in question
% Output:    
%   values:  
%       value of attribute asked for.  Returns the empty matrix 
%       in case of an error.  There is an ambiguity in the case of 
%       NC_BYTE data, so it is always retrieved as an int8 datatype.
%       If you wanted uint8, then you must cast it yourself.
%
% Example:
%    values = nc_attget('foo.cdf', 'x', 'scale_factor')
%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% $Id: nc_attget.m 1870 2006-10-09 12:08:53Z johnevans007 $
% $LastChangedDate: 2006-10-09 08:08:53 -0400 (Mon, 09 Oct 2006) $
% $LastChangedRevision: 1870 $
% $LastChangedBy: johnevans007 $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


if nargin < 3
	error ( '%s:  need at least three input arguments.\n', mfilename );
end




%
% Do we use java instead of mexnc?
if strcmp ( nc_method, 'java' )
	values = nc_attget_java ( ncfile, varname, attribute_name );
	return
end




[ncid, status] =mexnc('open', ncfile, nc_nowrite_mode );
if ( status ~= 0 )
	ncerror = mexnc ( 'strerror', status );
	error ( '%s:  mexnc:open failed on %s (%s).\n', mfilename, ncfile, ncerror );
end

%
% If the user passed in the name 'GLOBAL' then we need to check
% that 'GLOBAL' is not the name of a variable.  If it is not, then
% the user must have meant global attribute.
%
% Assume at first that the user did not mean a global attribute.  Then
% test all the conditions under which this could be invalidated.
user_intent_is_global=0;
lvarname = lower(varname);
if ( (ischar(varname)) & ( strcmp(lvarname,'global') | strcmp(lvarname,'nc_global') ) )
	[varid, status] = mexnc ( 'inq_varid', ncid, varname );
	if ( status ~= 0 )
		%
		% There is NO variable called 'global'.
		user_intent_is_global=1;
		varname = 'NC_GLOBAL';
	end
end

%
% -1 is the varid corresponding to NC_GLOBAL.
if ( (isnumeric(varname)) & (varname == -1) )
	user_intent_is_global = 1;
end

%
% I suppose that '' is invalid as a variable name, so
% it could work to signify a global attribute.
if ( (ischar(varname)) & ( strcmp(varname, '') ) )
	user_intent_is_global = 1;
	varid = nc_global;
end

if user_intent_is_global
	varid = nc_global;
	varname = 'NC_GLOBAL';
else
	[varid, status] = mexnc ( 'inq_varid', ncid, varname );
	if ( status ~= 0 )
		mexnc('close',ncid);
		ncerror = mexnc ( 'strerror', status );
		fmt = '%s:  ''inq_varid'' failed, ''%s''.\n';
		error ( fmt, mfilename, ncerror );
	end
end



[dt, status]=mexnc('inq_atttype',ncid,varid,attribute_name);
if ( status ~= 0 )

	mexnc('close',ncid);
	ncerr = mexnc ( 'strerror', status );
	error ( ncerr );

end

datatype_string = nc_datatype_string(dt);
switch ( datatype_string )
case 'NC_DOUBLE'
	[values, status]=mexnc('get_att_double',ncid,varid,attribute_name);
case 'NC_FLOAT'
	[values, status]=mexnc('get_att_float',ncid,varid,attribute_name);
case 'NC_INT'
	[values, status]=mexnc('get_att_int',ncid,varid,attribute_name);
case 'NC_SHORT'
	[values, status]=mexnc('get_att_short',ncid,varid,attribute_name);
case 'NC_BYTE'
	[values, status]=mexnc('get_att_schar',ncid,varid,attribute_name);
case 'NC_CHAR'
	[values, status]=mexnc('get_att_text',ncid,varid,attribute_name);
otherwise
	format = '%s:  unrecognized datatype %s.\n';
	error ( format, mfilename, datatype_string );
end

if ( status ~= 0 )
	ncerror = mexnc ( 'strerror', status );
	mexnc('close',ncid);
	error ( '%s:  ''get_att____'' failed on %s, %s, %s, (%s).\n', ...
	          mfilename, ncfile, varname, attribute_name, ncerror );
end

status = mexnc('close',ncid);
if ( status ~= 0 )
	ncerror = mexnc ( 'strerror', status );
	error ( '%s:  ''close'' failed on %s, (%s).\n', ...
	          mfilename, ncfile, ncerror );
end


return;















