function return_status = nc_varrename ( ncfile, old_variable_name, new_variable_name )
% NC_VARRENAME:  Renames a NetCDF variable.
%
% USAGE:  status = nc_varrename ( netcdf_file, old_variable_name, new_variable_name );
%
% PARAMETERS:
% Input:
%     netcdf_file:  
%        Input netcdf file name.
%     old_variable_name:
%        Name of variable that we wish to rename.
%     new_variable_name:
%        What we want to rename it to.  Dangling participle, I know.  So sue me, Chester.
% Output:
%     status:
%        -1 if process fails
%         0 if process succeeds
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% $Name:  $
% $Id: nc_varrename.m,v 1.1 2005/06/29 16:07:00 brian Exp $
% AUTHOR:  johnevans@acm.org
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%







%
% assume failure until success is known
return_status = -1;

varnames = [];


% Show usage if too few arguments.
%
if nargin~=3 
   help nc_varrename;
   return;
end


%
% Open netCDF file
%
[ncid,status ]=mexnc('OPEN',ncfile,nc_write_mode);
if status < 0
	fprintf ( 2, '%s:  mexnc:open failed on %s.\n', mfilename, ncfile );
	return
end

% Suppress warning messages from netCDF
[status]=mexnc('setopts',0);

%
% Put into redef mode
status = mexnc('REDEF', ncid);
if status < 0
	fprintf ( 2, '%s:  mexnc:redef failed on %s.\n', mfilename, ncfile );
	mexnc('close',ncid);
    return
end



% 
% Find the old variable.
[varid, status] = mexnc('INQ_VARID', ncid, old_variable_name);
if status < 0
	fprintf ( 2, '%s:  mexnc:varid failed on %s:%s.\n', mfilename, ncfile, old_variable_name );
	mexnc('close',ncid);
    return
end


status = mexnc('RENAME_VAR', ncid, varid, new_variable_name);
if status < 0
	fprintf ( 2, '%s:  mexnc:varrename failed on %s:%s.\n', mfilename, ncfile, old_variable_name );
	mexnc('close',ncid);
    return
end

%
% Leave redef mode
status = mexnc('END_DEF', ncid);
if status < 0
	fprintf ( 2, '%s:  mexnc:endef failed on %s.\n', mfilename, ncfile );
	mexnc('close',ncid);
    return
end

mexnc('close',ncid);

return_status = 0;

return;

