function [lon,lat,topo] = read_topo (topo_file);
%
% [lon,lat,topo] = read_topo (topo_file);
%
% This function reads the requested topography file.

%------------------------------------------------------------------------------
%  Initialize output.
%------------------------------------------------------------------------------

lon = [];
lat = [];
topo = [];

%------------------------------------------------------------------------------
%  Open input netCDF file.
%------------------------------------------------------------------------------

ncid = netcdf (topo_file);

if isempty(ncid);
   disp (' ');
	disp ('***Error:  READ_TOPO - unable to open input netCDF file:');
	disp (['           ',setstr(34),topo_file,setstr(34)]);
	disp (' ');
	return;
end;

%------------------------------------------------------------------------------
%  Read input netCDF file.
%------------------------------------------------------------------------------

%----------------------------
%--- Read longitude data. ---
%----------------------------

name   = 'topo_lon';

londat = ncid{name}(:,1);

if isempty(londat);
   disp (' ');
	disp (['***Error:  READ_TOPO - unable to read ',setstr(34),name, ...
			 setstr(34),' from input netCDF file:']);
	disp (['           ',setstr(34),topo_file,setstr(34)]);
	disp (' ');
	return;
end;

%---------------------------
%--- Read latitude data. ---
%---------------------------

name   = 'topo_lat';

latdat = ncid{name}(:,2);

if isempty(latdat);
   disp (' ');
	disp (['***Error:  READ_TOPO - unable to read ',setstr(34),name, ...
			 setstr(34),' from input netCDF file:']);
	disp (['           ',setstr(34),topo_file,setstr(34)]);
	disp (' ');
	return;
end;

%-----------------------------
%--- Read topography data. ---
%-----------------------------

name   = 'topo';

topdat = ncid{name}(:);

if isempty(topdat);
   disp (' ');
	disp (['***Error:  READ_TOPO - unable to read ',setstr(34),name, ...
			 setstr(34),' from input netCDF file:']);
	disp (['           ',setstr(34),topo_file,setstr(34)]);
	disp (' ');
	return;
end;

clear name

%------------------------------------------------------------------------------
%  Close input netCDF file.
%------------------------------------------------------------------------------

status = close (ncid);

if ~isempty(status);
   disp (' ');
	disp ('***Error:  READ_TOPO - unable to close input netCDF file:');
	disp (['           ',setstr(34),topo_file,setstr(34)]);
	disp (' ');
	return;
end;

clear ncid status topo_file;

%------------------------------------------------------------------------------
%  Extract output.
%------------------------------------------------------------------------------

[ny nx] = size(topdat);

lon = londat(1) + londat(2).*(0:(nx-1));
lat = latdat(1) + latdat(2).*(0:(ny-1));
topo = topdat;

clear latdat londat nx ny topdat;
