function [data, long_name, units] = mcgetvar(file, var_name, corner, edges);
%       function [data, long_name, units] = 
%			mcgetvar(file, var_name, corner, edges);
%
%       Loads data from a netCDF datafile.  If corner and edges
%       are omitted, all data is retrieved.
%
%       data = variable requested by 'name'
%		long_name = the netCDF long name
%		units = the units
%       file = netCDF data file name and path
%       var_name = variable name to load
%       corner = starting index
%       edges = length of hyperslab
%
%       WARNING: this function assumes that corners and edges are correct.
%       the unlimited index always comes first
%
%       Written by Marinna Martini, U.S. Geological Survey
%
mexcdf('SETOPTS',0);
if nargin ~= 2 
        if nargin ~= 4,
                error ('Incorrect number of input arguments');
        end
end

cdfid = mexcdf('OPEN', file, 'NOWRITE');
if cdfid < 0, error(['Cannot open file ',file]); end

%       get global info
[ndims, nvars, natts, recdim, rcode] = mexcdf('INQUIRE', cdfid);
varid = mexcdf('VARID', cdfid, var_name);
if varid < 0, rcode = varid; 
	mexcdf('CLOSE', cdfid);
	error([var_name, ' does not exist in ', file]);	
end

if exist('corner')~=1 | exist('edges')~=1,
		% find out how many dims this variable has
		[junk, type, nvdims, vdims, nvatts, rcode] = mexcdf('VARINQ', cdfid, varid);
        corner = zeros(nvdims,1);
        edges = ones(nvdims,1);
		% set the dimensions
		for i=1:nvdims,
	        [junk, edges(i), rcode] = mexcdf('DIMINQ', cdfid, i-1);
	    end	
end

% grab the whole data set!
[data, rcode] = mexcdf('VARGET', cdfid, var_name, corner, edges);

long_name = mexcdf('ATTGET',cdfid,var_name,'long_name');
units = mexcdf('ATTGET',cdfid,var_name,'units');

mexcdf('CLOSE', cdfid);

