function nc_attput ( ncfile, varname, attribute_name, attval )
% NC_ATTPUT:  put an attribute into a netcdf file
%
% USAGE:  status = nc_attput ( ncfile, varname, attribute_name, attval );
%
% PARAMETERS:
% Input:
%    ncfile:  name of netcdf file being altered
%    varname:  
%        name of variable being altered.  
%        'GLOBAL' or '' denotes a global attribute.
%        If a numeric -1 is passed in, this will also indicate
%        a global attribute.
%    attribute_name:  name of attribute being altered
%    attval:  value of attribute to be written
% Output:
%    None.  In case of an error, an exception is thrown.
%
%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% $Id: nc_attput.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 ~= 4
	error ( '%s:  must have 4 input arguments.\n\n', mfilename );
end



[ncid, status] =mexnc( 'open', ncfile, nc_write_mode );
if  status ~= 0 
	ncerror_msg = mexnc ( 'strerror', status );
	fmt = '%s:  mexnc failed on %s with ''write'' mode, (%s).\n\n';
	error ( fmt, mfilename, ncfile, ncerror_msg );
end


%
% Put into define mode.
status = mexnc ( 'redef', ncid );
if ( status ~= 0 )
	mexnc ( 'close', ncid );
	ncerror_msg = mexnc ( 'strerror', status );
	fmt = '%s:  mexnc operation ''redef'' failed on %s (%s).\n\n';
	error ( fmt, mfilename, ncfile, ncerror_msg );
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, '') ) )
	varid = -1;
end

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

switch class(attval)

	case 'double'
		status = mexnc ( 'put_att_double', ncid, varid, attribute_name, nc_double, length(attval), attval);
	case 'single'
		status = mexnc ( 'put_att_float', ncid, varid, attribute_name, nc_float, length(attval), attval);
	case 'int32'
		status = mexnc ( 'put_att_int', ncid, varid, attribute_name, nc_int, length(attval), attval);
	case 'int16'
		status = mexnc ( 'put_att_short', ncid, varid, attribute_name, nc_short, length(attval), attval);
	case 'int8'
		status = mexnc ( 'put_att_schar', ncid, varid, attribute_name, nc_byte, length(attval), attval);
	case 'uint8'
		status = mexnc ( 'put_att_uchar', ncid, varid, attribute_name, nc_byte, length(attval), attval);
	case 'char'
		status = mexnc ( 'put_att_text', ncid, varid, attribute_name, nc_char, length(attval), attval);
	otherwise
		mexnc ( 'close', ncid );
		error ( '%s:  attribute class %s is not handled.\n\n', mfilename, ncfile );
end



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



%
% End define mode.
status = mexnc ( 'end_def', ncid );
if ( status ~= 0 )
	mexnc ( 'close', ncid );
	ncerror_msg = mexnc ( 'strerror', status );
	fmt = '%s:  mexnc operation ''endef'' failed on %s, ''%s''.\n\n';
	error ( fmt, mfilename, ncfile, ncerror_msg );
end


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


return;



