function test_inq_var ( ncfile )
% TEST_INQ_VAR
%
% Tests number of dimensions, variables, global attributes, record dimension for
% foo.nc.  Also tests helper routines, "nc_inq_ndims", "nc_inq_nvars", "nc_inq_ncatts".
%
% Tests bad ncid as well.
%

%
% Create a netcdf file with
[ncid, status] = mexnc ( 'create', ncfile, nc_clobber_mode );
if ( status < 0 )
	error ( 'CREATE failed' );
end


%
% DIMDEF
[xdimid, status] = mexnc ( 'def_dim', ncid, 'x', 20 );
if ( status < 0 )
	error ( 'DEF_DIM failed on X' );
end
[ydimid, status] = mexnc ( 'def_dim', ncid, 'y', 24 );
if ( status < 0 )
	error ( 'DEF_DIM failed on y' );
end
[zdimid, status] = mexnc ( 'def_dim', ncid, 'z', 32 );
if ( status < 0 )
	error ( 'DEF_DIM failed on z' );
end


%
% VARDEF
[xdvarid, status] = mexnc ( 'def_var', ncid, 'x_double', 'double', 1, xdimid );
if ( status < 0 )
	error ( 'DEF_VAR failed on x_double' );
end


%
% Define some attributes
attvalue = 'this is a test';
attlen = length(attvalue);
status = mexnc ( 'put_att_text', ncid, xdvarid, 'test_variable_attributes', 'char', attlen, attvalue );
if ( status < 0 )
	ncerr = mexnc ( 'strerror', status );
	err_msg = sprintf ( '%s:  put_att_double failed on variable attribute, ''%s''\n', mfilename, ncerr );
	error ( err_msg );
end


%
% ENDEF
[status] = mexnc ( 'end_def', ncid );
if ( status < 0 )
	error ( 'ENDEF failed with write' );
end



%
% INQ_VAR
[name, datatype, ndims, dimids, natts, status] = mexnc('INQ_VAR', ncid, xdvarid);
if ( status < 0 )
	msg = sprintf ( '%s:  INQ_VAR failed return status\n', mfilename );
	error ( msg );
end
if ~strcmp ( 'x_double', name )
	msg = sprintf ( '%s:  INQ_VARNAME failed\n', mfilename, ncerr );
	error ( msg );
end
if ( datatype ~= 6 )
	msg = sprintf ( '%s:  INQ_VAR failed on datatype\n', mfilename );
	error ( msg );
end
if ( ndims ~= 1 )
	msg = sprintf ( '%s:  INQ_VAR failed on ndims\n', mfilename );
	error ( msg );
end
if ( dimids ~= 0 )
	msg = sprintf ( '%s:  INQ_VAR failed on dimids\n', mfilename );
	error ( msg );
end
if ( natts ~= 1 )
	msg = sprintf ( '%s:  INQ_VAR failed on variable attributes\n', mfilename );
	error ( msg );
end


%
% Try a bogus case
[name, datatype, ndims, dimids, natts, status] = mexnc('INQ_VAR', -1, 0);
if ( status >= 0 )
	error ( 'INQ_VAR return status did not signal an error on bogus ncid case' );
end
[name, datatype, ndims, dimids, natts, status] = mexnc('INQ_VAR', ncid, -1);
if ( status >= 0 )
	error ( 'INQ_VAR return status did not signal an error on bogus varid case' );
end



fprintf ( 1, 'INQ_VAR succeeded\n' );


%
% INQ_VARNAME
[name, status] = mexnc('INQ_VARNAME', ncid, xdvarid);
if ( status < 0 )
	msg = sprintf ( '%s:  INQ_VARNAME failed return status\n', mfilename );
	error ( msg );
end
if ~strcmp ( 'x_double', name )
	msg = sprintf ( '%s:  INQ_VARNAME failed\n', mfilename, ncerr );
	error ( msg );
end


%
% Try a bogus case
[name, status] = mexnc('INQ_VARNAME', -1, 0);
if ( status >= 0 )
	error ( 'INQ_VARNAME return status did not signal an error on bogus ncid case' );
end
[name, status] = mexnc('INQ_VARNAME', ncid, -1);
if ( status >= 0 )
	error ( 'INQ_VARNAME return status did not signal an error on bogus varid case' );
end



fprintf ( 1, 'INQ_VARNAME succeeded\n' );





%
% INQ_VARTYPE
[datatype, status] = mexnc('INQ_VARTYPE', ncid, xdvarid);
if ( status < 0 )
	msg = sprintf ( '%s:  INQ_VARTYPE failed return status\n', mfilename );
	error ( msg );
end
if ( datatype ~= 6 )
	msg = sprintf ( '%s:  INQ_VARTYPE failed on datatype\n', mfilename );
	error ( msg );
end


%
% Try a bogus case
[datatype, status] = mexnc('INQ_VARTYPE', -1, 0);
if ( status >= 0 )
	error ( 'INQ_VARTYPE return status did not signal an error on bogus ncid case' );
end
[datatype, status] = mexnc('INQ_VARTYPE', ncid, -1);
if ( status >= 0 )
	error ( 'INQ_VARTYPE return status did not signal an error on bogus varid case' );
end



fprintf ( 1, 'INQ_VARTYPE succeeded\n' );

%
% INQ_VARNDIMS
[ndims, status] = mexnc('INQ_VARNDIMS', ncid, xdvarid);
if ( status < 0 )
	msg = sprintf ( '%s:  INQ_VARNDIMS failed return status\n', mfilename );
	error ( msg );
end
if ( ndims ~= 1 )
	msg = sprintf ( '%s:  INQ_VARNDIMS failed\n', mfilename );
	error ( msg );
end


%
% Try a bogus case
[ndims, status] = mexnc('INQ_VARNDIMS', -1, 0);
if ( status >= 0 )
	error ( 'INQ_VARNDIMS return status did not signal an error on bogus ncid case' );
end
[ndims, status] = mexnc('INQ_VARNDIMS', ncid, -1);
if ( status >= 0 )
	error ( 'INQ_VARNDIMS return status did not signal an error on bogus varid case' );
end



fprintf ( 1, 'INQ_VARNDIMS succeeded\n' );




%
% INQ_VARDIMID
[dimids, status] = mexnc('INQ_VARDIMID', ncid, xdvarid);
if ( status < 0 )
	msg = sprintf ( '%s:  INQ_VARDIMID failed return status\n', mfilename );
	error ( msg );
end
if ( dimids ~= 0 )
	msg = sprintf ( '%s:  INQ_VARDIMID failed\n', mfilename );
	error ( msg );
end


%
% Try a bogus case
[ndims, status] = mexnc('INQ_VARDIMID', -1, 0);
if ( status >= 0 )
	error ( 'INQ_VARDIMID return status did not signal an error on bogus ncid case' );
end
[ndims, status] = mexnc('INQ_VARDIMID', ncid, -1);
if ( status >= 0 )
	error ( 'INQ_VARDIMID return status did not signal an error on bogus varid case' );
end



fprintf ( 1, 'INQ_VARDIMID succeeded\n' );




%
% INQ_VARNATTS
[natts, status] = mexnc('INQ_VARNATTS', ncid, xdvarid);
if ( status < 0 )
	msg = sprintf ( '%s:  INQ_VARNATTS failed return status\n', mfilename );
	error ( msg );
end
if ( natts ~= 1 )
	msg = sprintf ( '%s:  INQ_VARNATTS failed\n', mfilename );
	error ( msg );
end


%
% Try a bogus case
[ndims, status] = mexnc('INQ_VARNATTS', -1, 0);
if ( status >= 0 )
	error ( 'INQ_VARNATTS return status did not signal an error on bogus ncid case' );
end
[ndims, status] = mexnc('INQ_VARNATTS', ncid, -2);
if ( status >= 0 )
	error ( 'INQ_VARNATTS return status did not signal an error on bogus varid case' );
end



fprintf ( 1, 'INQ_VARNATTS succeeded\n' );





status = mexnc ( 'close', ncid );
if ( status < 0 )
	ncerr = mexnc ( 'strerror', status );
	msg = sprintf ( '%s:  %s\n', mfilename, ncerr );
	error ( msg );
end

return











