function [topog_new, lon_new] = m_fixlon(lon, lat, topog, lonrange)
%
% [topog_new, lon_new] = m_fixlon(lon, lat, topog, lonrange)
%
% Trims a topography array in longitude, and interpolates to provide
% first and last columns that exactly coincide with the longitude
% range to be plotted.  This is a quick fix for a problem with the
% clipping in m_map, which would otherwise give topography contour
% maps with blank regions at the east and west borders.
%
% Inputs:
%     lon, lat  : row vectors with the X and Y grids
%     topog     : matrix of elevations on the lon, lat grid,
%                 n_lon X n_lat
%     lonrange  : 2-element array with longitude limits of map 
%                 NOTE: lonrange limits are used for clipping and must
%                 be inside the range of the vector lon 
%
% Outputs:
%     topog_new : modified topog matrix
%     lon_new   : corresponding X grid (row vector)
%
% This function has no error checking at all.  Ensure your lon and lat are 1XN

% 2001/05/25 EF

str = [];
if (length(lon) ==1) 
   str = [str, sprintf('(%d lon pts)', length(lon))];
end
if (length(lat) ==1) 
   str = [str, sprintf('(%d lat pts)', length(lon))];
end
if ~isempty(str)
   topog_new = topog;
   lon_new = lon;
   fprintf('m_fixlon returning unchanged:%s \n',str)
   return
end

   
   

newtopog = interp2(lon(:)', lat(:), topog, lonrange, lat(:));

ilon = find(lon<lonrange(2) & lon>lonrange(1));
topog_new = [newtopog(:,1), topog(:,ilon), newtopog(:,2)];
lon_new = [lonrange(1), lon(ilon), lonrange(2)];
