function values = nc_attget_java(ncfile, varname, attribute_name )
% NC_ATTGET_JAVA:  This function retrieves an attribute using the java API

import ucar.nc2.dods.*    % import opendap reader classes
import ucar.nc2.*         % have to import this as well for NetcdfFile
                           % Now that's just brilliant.  

%
% Open netCDF file
%

if snc_is_url ( ncfile )
	try
		jncid = DODSNetcdfFile(ncfile);
	catch
		strformat = '%s:  DODSNetcdfFile failed on url, ''%s''.\n';
		error ( strformat, mfilename, ncfile, lasterr ); 
	end
else
	try
		jncid = NetcdfFile(ncfile);
	catch
		strformat = '%s:  NetcdfFile failed on url, ''%s''.\n';
		error ( strformat, mfilename, ncfile, lasterr ); 
	end
end



if ischar ( varname )

	%
	% Did the user pass us 'global' or 'nc_global'?  If so, and
	% if there is no root group variable called global, then 
	% they really meant a global attribute.
	lvarname = lower(varname);
	if isempty(lvarname)

		%
		% The user said ''.
		jatt = jncid.findGlobalAttribute ( attribute_name );

	elseif strcmp(lvarname,'global') && nc_isvar ( ncfile, varname )

		%
		% The user said 'global' and there actually was a variable
		% called 'global'.  
		jvarid = jncid.findVariable(varname);
		jatt = jvarid.findAttribute ( attribute_name );

	elseif strcmp(lvarname,'global') && ~nc_isvar ( ncfile, varname )

		%
		% The user said 'global', but there is no variable 
		% called 'global', so it must to a global attribute.
		jatt = jncid.findGlobalAttribute ( attribute_name );

	else

		%
		% Ok, it was just a regular variable.
		jvarid = jncid.findVariable(varname);
		jatt = jvarid.findAttribute ( attribute_name );

	end


else

	%
	% The user said something like -1? It must be a global attribute? 
	jatt = jncid.findGlobalAttribute ( attribute_name );
end

if isempty(jatt)
	close(jncid);
	error ( '%s:  Could not locate attribute %s', mfilename, attribute_name );
end

if ( jatt.isString() ) 
	values = jatt.getStringValue();
	values = char ( values );
else
	%
	% convert it to a numeric array.
	j_array = jatt.getValues();
	values = j_array.copyTo1DJavaArray();
	values = values';


	theDataTypeString = char ( jatt.getDataType.toString() ) ;
	switch ( theDataTypeString )
	case 'double'
		values = double(values);
	case 'float'
		values = single(values);
	case 'int'
		values = int32(values);
	case 'short'
		values = int16(values);
	case 'byte'
		values = int8(values);
	otherwise
		close(jncid);
		fmt = '%s:  unhandled attribute type ''%s'' for attribute ''%s''';
		error ( fmt, theDataTypeString, attribute_name );
	end
end

	close(jncid);

return


