function [fieldOut]=interp_llc270toXXXX_v7(fieldIn,flagXX,flagNZ,flaghFac,nx,rC0,rC1)

%------------
% function [fieldout]=interp_llc270toXXXX_v7(fieldIn,flagXX,flagNZ,flagFac,nx,rC0,rC1)
% fieldIn:  [nx ny nz nt] or [nx 1 nz nt] or [nx ny 1 nt] or [nx 1 1 1]
% dimXX = [1 1] or [1 0] or [0 1] for interp in x/y or x or y dir, [0 0]=no interp horiz
% flagNZ = [1]=yes z-interp in 3rd dim, else [0]=no, z-interp is to any random klev
% flaghFac = [0]=normal mapping, [1]=deal with partial cell
% nx: 540,1080,2160,4320
%          rr = nx_new/270
% fieldOut: [sz(1)*rr  sz(2)*rr (106)] or [sz(1)*rr+1 sz(1)*rr+1 (106)]
%------------

% Expect on the incoming that the dimension is already 3 or 4 dim?
% However, if the 2nd dim is equal to nz0, will add a 2nd dim of 1
% and slide everything down the line to 3rd and 4th dim

if(length(flagXX)~=2);error('need size flagXX to be 2x1, eg [1 1]');end;

nz=106;nz0=50;

%horizontal interp:
%first check size and make everything 4 dim: [nx ny nz nt]
sz0=size(fieldIn);L=length(sz0);
if(L==2);
  sz0=[sz0 1 1];
elseif(L==3);
  sz0=[sz0 1];
end;

%find which one is z-dir:
iz=find(sz0==nz0);
if(iz==2);
  if(sz0(3)>1);	%assume if this is the case, then sz0(3) is nt
    fieldIn=permute(fieldIn,[1 4 2 3]);
  else;
    fieldIn=permute(fieldIn,[1 3 2 4]);
  end;
  sz0=size(fieldIn);
  if(length(sz0)==3);sz0=[sz0 1];end;
end;

if(sum(flagXX)>0);
  fac=nx/270;
  if(flagXX(1)==1);nx=sz0(1)*fac;else;nx=sz0(1);end;
  if(flagXX(2)==1);ny=sz0(2)*fac;else;ny=sz0(2);end;
  newF=zeros(nx,ny,sz0(3),sz0(4));
  for j=1:ny/sz0(2);
    for i=1:nx/sz0(1);
      newF(i:nx/sz0(1):nx,j:ny/sz0(2):ny,:,:)=fieldIn;
    end;
  end;
else;
  newF=fieldIn;
end;

if(flagNZ==1);
    clear tempk temp1 temp3 temp4 temp5
    %vertical interp:
    sz=size(newF);if(length(sz)==2);sz=[sz 1 1];elseif(length(sz)==3);sz=[sz 1];end;
    iz=find(sz==nz0);
    if(length(iz)==0);error('wrong size in 3rd dimension');end;

    temp4=zeros(sz(1),sz(2),nz,sz(4));

    for k=1:nz
      clear iz ss dd w
      iz=closest(rC1(k),rC0,2);
      iz=sort(iz);
      ss=sign(rC0(iz)-rC1(k));
      if(ss(1)==ss(2));     %if both closest pts are on the same side, then take the 1st depth
        w=[1 0];
      else;
        dd=abs(diff(rC0(iz)));       %
        w=1-abs([rC0(iz)-rC1(k)])./dd;
      end;
      fprintf('%i %f %f ',k,w);
      if(mod(k,10)==0);fprintf('\n');end;
      temp4(:,:,k,:)=w(1).*newF(:,:,iz(1),:)+w(2).*newF(:,:,iz(2),:);
    end;

    %fprintf('\n');
    fieldOut=temp4;
else
    fieldOut=newF;
end;

%squeezing out extra dimensions:
fieldOut=squeeze(fieldOut);

return
