function [varargout] = getnc3(varargin);


%[var, lat, lon, level, time]= getnc3(filin, varn, lims, lev, tim);  %spatial three dimensional data
%[var, lat, lon,  time]=getnc3(filin, varn, lims, tim);       %spatial 2d data
%numberinputs=nargin

if nargchk(4, 5, nargin);
  error('Check input arguments - must have between four and five')
end

filin= varargin{1};
varn = varargin{2};
lims = varargin{3};
if nargin==4
    tim = varargin{4};
else
    lev = varargin{4};
    tim = varargin{5};
end

%for the case where the longitude domain goes through 360 degrees
minlon=lims(1,1);
maxlon=lims(1,2);
minlat=lims(1,3);
maxlat=lims(1,4);


if maxlon > minlon
    error('this program is only intended for case where the longitude domain passes through 360')
end



if nargin == 5
    [varl, lat, lonl, level, time] = getnc2(filin, varn,[minlon 360 minlat maxlat], lev, tim);
    [varr, lat, lonr, level, time] = getnc2(filin, varn,[0 maxlon minlat maxlat], lev, tim);
else
    [varl, lat, lonl, time] = getnc2(filin, varn,[minlon 360 minlat maxlat], tim);
    [varr, lat, lonr, time] = getnc2(filin, varn,[0 maxlon minlat maxlat], tim);
end
if nargin == 5
    a=shiftdim(varl,3);
    b=shiftdim(varr,3);
    var=[a;b];
    var=shiftdim(var,1);
    varargout{4} = level;
    varargout{5} = time;
    

else
    a=shiftdim(varl,2);
    b=shiftdim(varr,2);
    var=[a;b];
    var=shiftdim(var,1);
    varargout{4} = time;
end
lon=[lonl;lonr];

varargout{1} = var;
varargout{2} = lat;
varargout{3} = lon;


