function status = check_cdf(cdf)

%--------------------------------------------------------------------
%     Copyright J. V. Mansbridge, CSIRO, january 24 1992
% CHANGE   1.1 92/01/29
%
% status = check_cdf(cdf)
%
% DESCRIPTION:
%  This checks whether the file cdf is accessible.  It returns a
%  status flag of zero if the file cdf
%  exists.  If the file doesn't exist but cdf.Z does exist then the user is
%  prompted about uncompressing the file and if the uncompression is done
%  successfully then a zero status flag is returned.  If the uncompression
%  is not wanted by the user or it fails or there is no compressed file then
%  a status flag of 1 is returned.
%  Note that there will be trouble if the operating system does not
%  have the Unix uncompress facility.
% 
% INPUT:
%  cdf is the name of a netCDF file but without the .cdf or .cdf.Z extent.
%
% OUTPUT:
%  status, an error flag; = 0 if the netCDF file exists or is
%  successfully uncompressed; = 1 if it doesn't exist or the user does
%  not want the compressed file to be uncompressed.
%
% EXAMPLE:
%  status = check_cdf('fred')
%
% CALLER:   getcdf.m, inqcdf.m
% CALLEE:   Unix uncompress
%
% AUTHOR:   J. V. Mansbridge, CSIRO
%---------------------------------------------------------------------

% @(#)check_cdf.m   1.1   92/01/29
% 
%--------------------------------------------------------------------

comp = [cdf '.Z'];
if exist(cdf) == 2
   status = 0;
elseif exist(comp) == 2

% The following commented code allows the user to choose to have the file
% uncompressed.

   s = [ cdf ' is compressed.  Do you want it to be uncompressed?' ...
       '  (y/n) [y] ' ];
   compit = input(s, 's');

% compit is converted to 'y' if it is empty or 'Y'.  Note that the
% commented out code does not work as I expected.  This is because
% when compit is empty then compit == 'Y' returns an empty array which
% produces an empty array when combined in a logical or with the true
% statement isempty(compit).  This is interpreted as false.

%   if isempty(compit) == 1 | compit == 'Y'
%		compit = 'y';
%   end

   if isempty(compit)
		compit = 'y';
   elseif compit == 'Y'
		compit = 'y';
   end
   if compit == 'y'
		s = [ '!uncompress ' comp ];
		eval(s)
		if exist(cdf) == 2
			status = 0;
		else
			status = 1;
		end
   else
      status = 1;
   end
else
   disp([ cdf ' not found in either compressed or uncompressed form' ])
   status = 1;
end

