function [lon, mean_lon, lon_split] = unwrap_lon(lon, lon_split);
% newlon = unwrap_lon(lon, lon_split);
% 
%  attempts to make longitude one continuous line where data exist
%  if lon_split is not specified, try to do it automatically
%    otherwise, make the split occur at lon_split


if (nargin == 1) %lon_split was not specified
   lon_split = [];
end


goodi = find(~isnan(lon));
if ~isempty(lon)
   if (isempty(lon_split))   %guess it
      slon = sin(pi*lon(goodi)/180);
      clon = cos(pi*lon(goodi)/180);
      mean_lon = 90 - (180/pi)*angle(mstdgap(slon) + i*mstdgap(clon));
      lon_split = mean_lon  - 180;
      lon_split = rem(mean_lon -360, 360) + 180;
   end
   
   lon = rem(lon - lon_split + 720  , 360) + lon_split ;
   lon = lon - 360*floor(max(lon(goodi))/360);
end
