function yn = nc_isvar ( ncfile, varname )
% NC_ISVAR:  yes if the given variable is in the netcdf file
%
% USAGE:  yn = nc_isvar ( ncfile, varname );
%
% PARAMETERS:
% Input:
%     ncfile:  
%        Input netcdf file name.
%     varname:  
%        variable to check
% Output:
%     yn:  
%        0 if the answer is no, 1 if yes.
%
% In case of an error, an exception is thrown.
%
%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% $Id: nc_isvar.m 1746 2006-08-31 15:30:40Z johnevans007 $
% $LastChangedDate: 2006-08-31 11:30:40 -0400 (Thu, 31 Aug 2006) $
% $LastChangedRevision: 1746 $
% $LastChangedBy: johnevans007 $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%
% Both inputs must be character
if nargin ~= 2
	error ( '%s:  must have two inputs.\n', mfilename );
end
if ~ischar(ncfile)
	error ( '%s:  first argument must be character.\n', mfilename );
end
if ~ischar(varname)
	error ( '%s:  second argument must be character.\n', mfilename );
end



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


% assume false until we know otherwise
yn = 0;




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


[varid, status] = mexnc('INQ_VARID', ncid, varname);
if ( status < 0 )
	yn = 0;
elseif varid >= 0
	yn = 1;
else
	error ( '%s:  unknown condition, INQ_VARID succeeded, but returned a negative varid???', mfilename );
end

mexnc('close',ncid);
return









function yn = nc_isvar_java ( ncfile, varname )
% assume false until we know otherwise
yn = 0;

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



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


jvarid = jncid.findVariable(varname);

%
% Did we find anything?
if ~isempty(jvarid)
	yn = 1;
end

close(jncid);

return

