% **************************************************************
% resize_lev_time.m
%
% interpolate data into different lev and time dimensions
% var is in dimension of (lon,lat,lev,time)
%
% by shuaiqi Tang
% 2014.11.11
% **************************************************************

function varnew = resize_lev_time(var,lev1,time1,lev2,time2)

levlen1=length(lev1);
timelen1=length(time1);
levlen2=length(lev2);
timelen2=length(time2);
lonlen=size(var,1);
latlen=size(var,2);

[~,~,c,d]=size(var);
if c*d~=levlen1*timelen1
    error('dimension does not match');
end
    

if levlen1==1   % change dimension of time only
    varnew = zeros(lonlen,latlen,levlen1,timelen2);
    for yy=1:latlen
        for xx=1:lonlen
            var_tmp=reshape(var(xx,yy,:,:),levlen1,timelen1);
                var2=interp1(time1,var_tmp,time2,'linear');
                varnew(xx,yy,:,:)=reshape(var2,1,1,levlen1,timelen2);
        end
    end
else    %change dimension of time and lev
    varnew = zeros(lonlen,latlen,levlen2,timelen2);
    [x1,y1] = meshgrid(time1,lev1);
    [x2,y2] = meshgrid(time2,lev2);
    for yy=1:latlen
        for xx=1:lonlen
            var_tmp=reshape(var(xx,yy,:,:),levlen1,timelen1);
                var2=interp2(x1,y1,var_tmp,x2,y2,'linear');
                varnew(xx,yy,:,:)=reshape(var2,1,1,levlen2,timelen2);
        end
    end
end
