function basemap(lonrange, latrange, varargin)
% basemap(lonrange, latrange, ...)
% EF 2003/09/28 modified to automatically calculate topog and coast
%    files as needed, and to save them
%
% varargin - [name, value] pairs
%
% name         values       default                     what
%------        -----       ----------                   --------
% save2mat      0,1            1               save extracted coastline
%                                                   and topog to mat file
%
% xy_pad       float           0               >= 0 : pad lon and lat by this
%                                              -1: use map_range
%
% toposource   'ss', 'best',    'ss'            source of topography
%                                              'ss' (smith sandwell)
%                                              'best' (best_topo.m)
% namefile
%
% namefontsize
%
% toponsub
%
% namefontsize

cfg.projection = 'merc';
cfg.save2mat = 1;
cfg.xy_pad = -1;
cfg.toposource = 'ss';
cfg.toponsub = 0; % 1, 3, 9 or 0; 0 means automatic
cfg.namefontsize = 8;
cfg.coastresolution = ''; % letter for gshhs*; '' means automatic
cfg.namefile = '';

cfg = fillstruct(cfg, varargin, 0);


clev = [-10000 -5000 -4000 -3000 -2000 -1000 -500 -100 20000];  % topo contour levels


if cfg.xy_pad < 0
   [xr,yr]=map_range(lonrange, latrange);
else
   xr = lonrange + cfg.xy_pad*[-1 1];
   yr = latrange + cfg.xy_pad*[-1 1];
end



lonlat = [floor(xr(1))   ceil(xr(2))  floor(yr(1)) floor(yr(2))];


if cfg.save2mat
   coastfile = sprintf('coast_%d_%d_%d_%d.mat', lonlat)
   topofile=   sprintf('topo_%d_%d_%d_%d.mat', lonlat)
else
   coastfile = 'tmpcoast.mat';
   topofile = 'tmptopo.mat';
end

drmax = max([diff(xr), diff(yr)]);

m_proj(cfg.projection, 'lon', lonrange, 'lat', latrange);

% set up colors, add topo and coastlines (fills coast)
cm = get(gcf, 'Colormap');
ncm = size(cm, 1); % number of pre-existing colors

topo_cmap = mk_cmap(length(clev), 'topo');
topo_irange = ncm + [1 size(topo_cmap,1)];
colormap([cm; topo_cmap])

if ~cfg.save2mat
   if (exist(coastfile,'file'))
      delete(coastfile)
   end
   if (exist(topofile,'file'))
      delete(topofile)
   end
end

if ~exist(topofile, 'file')
   if exist('ss_block') ~= 2
      addpath /home/noio/programs/topog/sstopo
   end

   if strcmp(cfg.toposource, 'ss')
      if cfg.toponsub == 0
         nsub = 1;
         if drmax > 20, nsub = 3; end
         if drmax > 60, nsub = 9; end
      else
         nsub = cfg.toponsub
      end
      [longrid, latgrid, topog] = ss_block(xr, yr, nsub);
   else
      [longrid, latgrid, topog] = best_topo(xr, yr);
   end
   save6('-mat', topofile, 'longrid', 'latgrid', 'topog')
end
load(topofile);


[topog_new, lon_new] = m_fixlon(longrid, latgrid, topog, lonrange);
[cmat, htopo]=m_contourf(lon_new, latgrid, topog_new, clev);
set(htopo, 'EdgeColor', 'none');

%cf_color(htopo, clev);
cdata_direct(htopo, clev, topo_irange(1));
hcb = cbar_direct(clev/1000, topo_irange(1), 'vert', 'Depth (km)', [0.2, 0.4]);
ytl = get(hcb, 'YTickLabel');
set(hcb, 'YTickLabel', num2str(-str2num(ytl)));


if ~exist(coastfile, 'file'),
   if isempty(cfg.coastresolution),
      reslet = 'i';
      if drmax > 20, reslet = 'l'; end
      if drmax > 60, reslet = 'c'; end
   else
      reslet = cfg.coastresolution;
   end
   feval(sprintf('m_gshhs_%c', reslet), 'save', coastfile);
end

load(coastfile);

m_usercoast(coastfile, 'patch', [0.7872    1.0000    0.6130]);

% set map axes style
m_grid('box', 'on', 'tickdir', 'out', 'linestyle', 'none');

%%% The following needs work; and/or being moved to a separate
%%% m-file.  Maybe use cell array for names. Or use a simple text file.
if ~isempty(cfg.namefile)
   namestruct = load(cfg.namefile);
   for ii=1:length(namestruct.nlon)
      h_names(ii) = m_text(namestruct.nlon(ii), namestruct.nlat(ii), ...
            namestruct.names(ii,:),...
            'FontSize', cfg.namefontsize, ...
            'clip', 'on', 'Color', 'r',...
            'tag', 'location_names');
   end
end


if ~cfg.save2mat
   if (exist(coastfile,'file'))
      delete(coastfile)
   end
   if (exist(topofile,'file'))
      delete(topofile)
   end
end

