% ----------
% usage: to run in matlab 
%   [top_lat, top_lon, topog] =  ...
%               get_topo(latmin, latmax, lonmin, lonmax);
%   [top_lat, top_lon, topog] =  ...
%               get_topo(latmin, latmax, lonmin, lonmax, sub_lat, sub_lon);
%   [top_lat, top_lon, topog] =  ...
%               get_topo(latmin, latmax, lonmin, lonmax, sub_both);
%   
%   sub_lat, sub_lon are integers to subsample the grid by: 
%      default is one (every grid point). 
%      Example: every third point in lat (sub_lat=3),  
%      every other point in lon (sub_lon=2).
%
%   output:
%         top_lat: m x 1
%	  top_lon: n x 1
%	  topog:   m x n
% ----------
% The original database has
%  	- a 5 minute (1/12 deg) resolution
%	- longitude ranges from -180 to 180 	(4321 points)
%	- latitude ranges from -78 to 90 	(2017 points)
% 	- depth is in meters, positive values
%	- land is masked as (-1) in the matlab variable ("topog")
%
% Note: lonmin must be less than lonmax(similarly latmin < latmax is required)
%	If you must cross the date line (e.g. 178W to 178E) use 
%	lonmin = -182, lonmax = -178 or lonmin=178, lonmax=182


function [topog_lat, topog_lon, topog]= topog(latmin, latmax, lonmin, lonmax, m, n);
	if (nargin < 4 )
		error('Need greater than 4 input arguments. Please input [latmin, latmax, lonmin, lonmax, m, n]');
	end;

	if (latmin > latmax | lonmin > lonmax)
		msg=sprintf('Note: lonmin must be less than lonmax(similarly latmin < latmax is required)\n 	If you must cross the date line (e.g. 178W to 178E) \n 	use lonmin = -182, lonmax = -178 or lonmin=178, lonmax=182');
		error(msg);
	end;	

	INPUTFILE = '/home/noio/programs/bathy/dbdb5.fin';

  	if (nargin == 4)
		m = 1;
		n = 1;
	elseif (nargin == 5)
		n = m;	
	end;

	[topog_lat, topog_lon, topog]=topo(INPUTFILE, latmin, latmax, lonmin, lonmax, m, n);


