function test_rename_var ( ncfile )
% TEST_RENAME_VAR:  Just creates a file with a variable called 'x'.  
%    Renames it to 'x2', then tests to see if it can see 'x2'.

%
% ok, first create this baby.
[ncid, status] = mexnc ( 'create', ncfile, nc_clobber_mode );
if ( status ~= 0 )
	ncerr_msg = mexnc ( 'strerror', status );
	msg = sprintf ( '%s:  ''create'' failed on %s, error message '' %s ''\n', mfilename, ncfile, ncerr_msg );
	error ( msg );
end


[xdimid, status] = mexnc ( 'def_dim', ncid, 'x', 10 );
if ( status ~= 0 )
	ncerr_msg = mexnc ( 'strerror', status );
	msg = sprintf ( '%s:  ''def_dim'' failed on dim x, file %s, error message '' %s ''\n', mfilename, ncfile, ncerr_msg );
	error ( msg );
end


[varid, status] = mexnc ( 'def_var', ncid, 'x', 'NC_FLOAT', 1, [xdimid] );
if ( status ~= 0 )
	ncerr_msg = mexnc ( 'strerror', status );
	msg = sprintf ( '%s:  ''def_var'' failed on var rh, file %s, error message '' %s ''\n', mfilename, ncfile, ncerr_msg );
	error ( msg );
end

status = mexnc ( 'end_def', ncid );
if ( status ~= 0 )
	ncerr_msg = mexnc ( 'strerror', status );
	msg = sprintf ( '%s:  ''end_def'' failed on file %s, error message '' %s ''\n', mfilename,  ncerr_msg );
	error ( msg );
end

status = mexnc ( 'close', ncid );
if ( status ~= 0 )
	msg = sprintf ( '%s:  CLOSE failed\n', mfilename );
	error ( msg );
end

[ncid, status] = mexnc ( 'open', ncfile, nc_write_mode );
if ( status ~= 0 )
	msg = sprintf ( '%s:  OPEN failed\n', mfilename );
	error ( msg );
end

status = mexnc('REDEF', ncid);
if ( status ~= 0 )
	msg = sprintf ( '%s:  REDEF failed (''%s'')\n', mfilename, mexnc ( 'strerror', status ) );
	error ( msg );
end

[varid, status] = mexnc('INQ_VARID', ncid, 'x');
if ( status ~= 0 )
	msg = sprintf ( '%s:  INQ_VARID failed\n', mfilename );
	error ( msg );
end

[status] = mexnc('RENAME_VAR', ncid, varid, 'x2');
if ( status ~= 0 )
	msg = sprintf ( '%s:  RENAME_VAR failed return status\n', mfilename );
	error ( msg );
end


%
% test non existance variable
[name, datatype, ndims, dimids, natts, status] = mexnc('INQ_VAR', ncid, varid);
if ( status ~= 0 )
	msg = sprintf ( '%s:  INQ_VAR failed\n', mfilename );
	error ( msg );
end

if ~strcmp(name, 'x2' )
	msg = sprintf ( '%s:  RENAME_VAR failed, new var name should have been %s, not %s\n', mfilename, 'x2', name  );
	error ( msg );
end

%
% CLOSE
status = mexnc ( 'close', ncid );
if ( status ~= 0 )
	msg = sprintf ( '%s:  CLOSE failed\n', mfilename );
	error ( msg );
end

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


return



