function [attribute, return_status] = nc_get_attribute_struct ( cdfid, varid, attnum )
% NC_GET_ATTRIBUTE_STRUCT:  Returns a NetCDF attribute as a structure
%
% You don't want to be calling this routine directly.  Just don't use 
% it.  Use nc_attget instead.  Go away.  Nothing to see here, folks.  
% Move along, move along.
%
% USAGE:  [attstruct, status] = nc_get_attribute_struct ( ncfile, cdfid, varid, attnum );
%
% PARAMETERS:
% Input:
%     cdfid:  NetCDF file id
%     varid:  NetCDF variable id
%     attnum:  number of attribute
% Output:
%     attstruct:  structure with "Name", "Nctype", "Attnum", and "Value" fields
%     status:  -1 if the routine fails, 0 if successful
%
% USED BY:  nc_getinfo.m, nc_getvarinfo.m
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% $Name:  $
% $Id: nc_get_attribute_struct.m,v 1.1 2005/06/29 16:07:00 brian Exp $
% AUTHOR:  johnevans@acm.org
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

return_status = -1;
attribute = [];

[attname, status] = mexnc('ATTNAME', cdfid, varid, attnum);
if status < 0 
	fprintf ( 2, '%s:  mexnc:attname failed on varid %d.\n', mfilename, varid );
	return
end

[att_datatype, dud, status] = mexnc('ATTINQ', cdfid, varid, attname);
if status < 0 
	fprintf ( 2, '%s:  mexnc:attinq failed on varid %d, attribute %s.\n', mfilename, varid, attname );
	return
end

[attval, status]=mexnc('attget',cdfid,varid,attname);
if status < 0 
	fprintf ( 2, '%s:  mexnc:attget failed on varid %d, attribute %s.\n', mfilename, varid, attname );
	return
end

%
% this puts the attribute into the variable structure
attribute.Name = attname;
attribute.Nctype = att_datatype;
attribute.Attnum = attnum;
attribute.Value = attval;

return_status = 0;

return

