function f270=interp_llc90_to_llc270(y90,f90,y270,fflag)
%-------
% function [f270,f270f]=interp_llc90_to_llc270(y90,f90,y270,fflag)
%
% reason: llc90 is not exactly parent of llc270 grid
% input: y90: llc90 YC ([4*nx0 4*nx0] lat) in world map view (patchface3D flag=2)
%        f90: llc90 [4*nx0 4*nx0 nz] field in world map view 
%        y270: llc270 YC ([4*nx 4*nx] lat) in world map view
%        fflag: 1 for step fxn, 0 for default 1/3,2/3 interp
% output: f270 [4*nx 4*nx nz]
%----------

%Example inputs:
%dir90='/net/nares/raid11/ecco-shared/ecco-version-4/output/GRID/';nx0=90;nz=50;
%dir270='/net/nares/raid8/ecco-shared/llc270/global/GRID/';        nx=270;
%dirIn='/net/nares/raid11/ecco-shared/ecco-version-4/input/input_all_from_pleiades/';
%f90=readbin([dirIn 'bathy_eccollc_90x50_min2pts.bin'],[nx0 nx0*13]);f90=patchface3D(nx0,nx0*13,1,f90,2);
%y90=readbin([dir90 'YC.data'],[nx0 nx0*13],1,'real*8');y90=patchface3D(nx0,nx0*13,1,y90,2);
%y270=readbin([dir270 'YC.data'],[nx nx*13]);y270=patchface3D(nx,nx*13,1,y270,2);

%get size
[nx0 ny0 nz]=size(f90);
[nx0p ny0p] =size(y90);
if(nx0~=ny0);fprintf('error: incorrect f90 size (%i %i)\n',[nx0 ny0]);return;end;
if(nx0p~=ny0p);fprintf('error: incorrect y90 size (%i %i)\n',[nx0p ny0p]);return;end;
if(nx0~=nx0p);fprintf('error: size f90 and y90 are not the same\n');return;end;
nx0=nx0/4;clear ny0 nx0p ny0p

[nx ny]=size(y270);
if(nx~=ny);fprintf('error: incorrect f270 size (%i %i)\n',[nx ny]);return;end;
nx=nx/4;

%defining field llc270:
f270=nan(nx*4,nx*4,nz);

%defining horizontal weights. Note "3" is b/c 270/3=90, if later on used for llc540 etc,
% need to change this hard-coded number with nskip=nx/nx0 for example.
if(fflag==0);w13=1/3;w23=2/3;elseif(fflag==1);w13=0;w23=1;end;

%scanning across horizontally
iy=1:3*nx;  iy0=1:3*nx0;

for ix=2:3:4*nx;
  ip=interp1(y270(ix,iy),iy,y90((ix+1)/3,iy0),'linear');
  for k=1:length(ip);iq(k)=closest(ip(k),iy,1);end;
  diq=diff(iq);
  f270(ix,iq,:)=f90((ix+1)/3,iy0,:);
  for k=1:length(iq)-1;
    w=zeros(1,diq(k)-1);for l=1:diq(k)-1;w(l)=l/diq(k);end;
    %if(fflag==1);w=round(w);i1=find(w==1);if(length(i1)>1);w(i1(1:end-1))=0;end;end;
    for l=1:diq(k)-1;
      if(fflag==0);
        f270(ix,iq(k)+l,:)=w(end-l+1).*f270(ix,iq(k),:)+(1-w(end-l+1)).*f270(ix,iq(k+1),:);
      elseif(fflag==1);
        f270(ix,iq(k)+l,:)=f270(ix,iq(k+1),:);	%bias up to 1 grid point
      end;
    end;
  end;
end;

%now horizontal interp:
ix=2:3:4*nx;
  f270(ix(2:end)-1,iy,:)=w13.*f270(ix(1:end-1),iy,:)+w23.*f270(ix(2:end),iy,:);
  f270(ix(1:end-1)+1,iy,:)=w23.*f270(ix(1:end-1),iy,:)+w13.*f270(ix(2:end),iy,:);

%wrapping on x-index 1 and 4*nx:
f270(1,iy,:)   =w23.*f270(2,iy,:)+w13.*f270(4*nx-1,iy,:);
f270(4*nx,iy,:)=w13.*f270(2,iy,:)+w23.*f270(4*nx-1,iy,:);

%now face 3:
ix3=1:nx;iy3=3*nx+1:4*nx;
f90f=f90(1:nx0,3*nx0+1:4*nx0,:);
f270f=f270(ix3,iy3,:);ix=2:3:nx;iy=2:3:nx;
f270f(ix,iy,:)=f90f;
  f270f(ix(2:end)-1,  iy,:)=w13.*f270f(ix(1:end-1),iy,:)+w23.*f270f(ix(2:end),iy,:);
  f270f(ix(1:end-1)+1,iy,:)=w23.*f270f(ix(1:end-1),iy,:)+w13.*f270f(ix(2:end),iy,:);
  f270f(:,iy(2:end)-1   ,:)=w13.*f270f(:,iy(1:end-1),:) +w23.*f270f(:,iy(2:end),:);
  f270f(:,iy(1:end-1)+1 ,:)=w23.*f270f(:,iy(1:end-1),:) +w13.*f270f(:,iy(2:end),:);

%face3/1 connection: readily available from global map:
ix=1:nx;iy=3*nx;
f270f(:,1,:) =w23.*f270f(:,2,:)+w13.*f270(ix,iy-1,:);
f270(ix,iy,:)=w13.*f270f(:,2,:)+w23.*f270(ix,iy-1,:);

%now the edge: face3/2 connection 
ix=nx+1:2*nx;iy=3*nx;
  temp=sym_g_mod(f270f,7,0);
  temp(:,1,:)  =w23.*temp(:,2,:)+w13.*f270(ix,iy-1,:);
  f270(ix,iy,:)=w13.*temp(:,2,:)+w23.*f270(ix,iy-1,:);
  f270f=sym_g_mod(temp,5,0);

%face3/4 connection (Bering Strait/Pacific opening)
ix=2*nx+1:3*nx;iy=3*nx;
  temp=sym_g_mod(f270f,6,0);
  temp(:,1,:)  =w23.*temp(:,2,:)+w13.*f270(ix,iy-1,:);
  f270(ix,iy,:)=w13.*temp(:,2,:)+w23.*f270(ix,iy-1,:);
  f270f=sym_g_mod(temp,6,0);

%face3/5 connection
ix=3*nx+1:4*nx;iy=3*nx;
  temp=sym_g_mod(f270f,5,0);
  temp(:,1,:)  =w23.*temp(:,2,:)+w13.*f270(ix,iy-1,:);
  f270(ix,iy,:)=w13.*temp(:,2,:)+w23.*f270(ix,iy-1,:);
  f270f=sym_g_mod(temp,7,0);

%put separate face3 back to global map
f270(ix3,iy3,:)=f270f;

%lastly, faces corners which are all land:
iy=3*nx;
for k=1:4;
 if(k==1);ix=1; f270(ix,iy,:)=(f270(ix+1,iy,:)+f270(4*nx,iy,:))./2;
 else
   if(k==2);ix=nx;elseif(k==3);ix=2*nx;elseif(k==4);ix=3*nx;end;
   f270(ix,iy,:)=(f270(ix-1,iy,:)+f270(ix+1,iy,:))./2;
 end;
end;

%first row, in Antarctica, should be all land
f270(:,1,:)=f270(:,2,:);

return

