function fileinfo = nc_info ( ncfile )
% NC_INFO:  information about a NetCDF 2 or 3 file
%
% USAGE:  fileinfo = nc_info ( ncfile );
%
% PARAMETERS:
% Input:
%    ncfile:  
%        a string that specifies the name of the NetCDF file
% Output:
%    fileinfo:
%        A structure whose fields contain information about the contants
%        of the NetCDF file.  The set of fields return in "fileinfo" are:
%
%        Filename:  
%            a string containing the name of the file.
%        Dimension:  
%            an array of structures describing the NetCDF dimensions.
%        DataSet:  
%            an array of structures describing the NetCDF datasets.
%        Attributes:  
%            An array of structures These correspond to the global attributes.
%
%
%        Each "Dimension" element contains the following fields:
%       
%        Name:
%            a string containing the name of the dimension.
%        Length:
%            a scalar value, the size of this dimension
%        Record_Dimension:
%            Set to 1 if the dimension is the record dimension, set to
%            0 otherwise.
%
%
%        Each "DataSet" element contains the following structures.
%
%        Name:  
%            a string containing the name of the variable.
%        Nctype:  
%            a string specifying the NetCDF datatype of this variable.
%        Dimensions:  
%            a cell array with the names of the dimensions upon which
%            this variable depends.
%        IsUnlimitedVariable:  
%            Flag, either 1 if the variable has an unlimited dimension
%            or 0 if not.
%        Rank:  
%            Array that describes the size of each dimension upon which 
%            this dataset depends.
%        DataAttributes:  
%            Same as "Attributes" above, but here they are the variable 
%            attributes.
%                         
%        Each "Attribute" or "DataAttribute" element contains the following 
%        fields.
%
%        Name:  
%            a string containing the name of the attribute.
%        Nctype:  
%            a string specifying the NetCDF datatype of this attribute.
%        Attnum:  
%            a scalar specifying the attribute id
%        Value: 
%            either a string or a double precision value corresponding to
%            the value of the attribute
%
%
% The "DataSet" elements are not populated with the actual data values.
%
% This routine purposefully mimics that of Mathwork's hdfinfo.
%
%
%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% $Id: nc_info.m 1870 2006-10-09 12:08:53Z johnevans007 $
% $LastChangedDate: 2006-10-09 08:08:53 -0400 (Mon, 09 Oct 2006) $
% $LastChangedRevision: 1870 $
% $LastChangedBy: johnevans007 $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



% Show usage if too few arguments.
%
if nargin<1 
	error ( '%s:  must have at least one input argument.\n', mfilename );
end


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




fileinfo.Filename = ncfile;




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



[ndims, nvars, ngatts, record_dimension, status] = mexnc('INQ', ncid);
if status < 0
	mexnc('close',ncid);
	error ( '%s:  mexnc:inq failed on %s.\n', mfilename, ncfile );
end


%
% retrieve the names of each dimension
if ndims == 0
	Dimension = struct ( [] );
else
	for dimid = 0:ndims-1

		[dimname{dimid+1,1}, status] = mexnc ( 'inq_dimname', ncid, dimid );
		if status < 0
			mexnc('close',ncid);
			error ( '%s:  mexnc:inq failed on %s.\n', mfilename, ncfile );
		end

	end
end


if nvars == 0
	fileinfo.DataSet = struct([]);
else
	for varid=0:nvars-1
		[varname{varid+1,1}, status] = mexnc ( 'inq_varname', ncid, varid );
		if status < 0
			mexnc('close',ncid);
			error ( '%s:  mexnc:inq_varname failed on %s.\n', mfilename, ncfile );
		end
	end
end

%
% Do the same for the global attributes
%
% get all the attributes
if ngatts == 0
	fileinfo.Attribute = struct([]);
else
	for attnum = 0:ngatts-1
		Attribute(attnum+1) = nc_get_attribute_struct ( ncid, nc_global, attnum );
	end
	fileinfo.Attribute = Attribute;
end





%
% Now go back and use snctools to get the dimension information 
% in a more abstract manner.
if ndims == 0
	Dimension = struct ( [] );
else
	for dimid = 0:ndims-1
		Dimension(dimid+1)=nc_getdiminfo ( ncid, dimid );
	end
end

%
% Now go back and use snctools to get the variable information 
% in a more abstract manner.
if nvars == 0
	DataSet = struct([]);
else
	for varid=0:nvars-1
		DataSet(varid+1) = nc_getvarinfo ( ncid, varid );
	end
end

fileinfo.Dimension = Dimension;
fileinfo.DataSet = DataSet;


mexnc('close',ncid);


return









