function [DATElocal,HOURSfgmt]=localtime(DATEgmt,LON);

% Converts GMT time to LOCAL time
% DATEgmt can be a mx6 matrix or a datenum vector
% LON should be between -180:180
% 

% Check dimensions of input 
nd=length(LON);
nt=size(DATEgmt);

% Check for dimentions of TIME/DATE vector
if length(nt)==2 % bug?, it was 2
    TIMEgmt=datenum(DATEgmt);
elseif length(nt)==1
    TIMEgmt=DATEgmt;
end

% Check LON range is between -180 and 180
rLON=range(LON);

if rLON(1)>=0
    display('Warning: LON should be between -180 and 180');
end

% Vector defining edges for TIME zones
dlon=15;
TIMEzone=-180-dlon/2:15:180+dlon/2;

[nLONbin, LONbin]=histc(LON,TIMEzone);

% Adjust BIN number to use as h before and after GMT time zone 
HOURSfgmt=LONbin-13;

% Variable to output
TIMElocal=TIMEgmt+HOURSfgmt./24;
DATElocal=datevec(TIMElocal);
    