function [FFrNf]=expand_llc_Ngrid(FF,nx,N,flagInterp)
%function [FFrNf]=expand_llc_Ngrid(FF,N)
%
% input:
%  FF : in 5 faces
%  nx : 90, 270, etc
%  N  : number of grid to downsize to
%  flagInterp: iterpolate or pick exact grid
%
% output: FFrNf: 5 faces of coarser grid
%

keyboard

sz0=size(FF{1},1);
i=1; i1=(i-1)*N+1;i2=i*N; ic1=(i2-i1)/2+1;         %needs to be an integer
if(floor(ic1)~=ic1);fprintf('ic1 = %f, not an integer, nothing is return',ic1);return;end;

nfx=[nx nx nx 3*nx 3*nx];
nfy=[3*nx 3*nx nx nx nx];

if(sz0==(nx+1));
  i1x=[1 1 1 2 2];  i2x=[nfx(1) nfx(2) nfx(3) nfx(4)+1 nfx(5)+1];
  j1y=[1 1 1 1 1];  j2y=[nfy(1) nfy(2) nfy(3) nfy(4)   nfy(5)  ];
else;
  i1x=[1 1 1 1 1];  i2x=[nfx(1) nfx(2) nfx(3) nfx(4)+0 nfx(5)+0];
  j1y=[1 1 1 1 1];  j2y=[nfy(1) nfy(2) nfy(3) nfy(4)   nfy(5)  ];
end;

%N=18;
clear FFout tmp

FFout=cat(1,FF{1}(i1x(1):i2x(1),j1y(1):j2y(1)),FF{2}(i1x(2):i2x(2),j1y(2):j2y(2)),...
            sym_g_mod(FF{4}(i1x(4):i2x(4),j1y(4):j2y(4)),7,0),...
            sym_g_mod(FF{5}(i1x(5):i2x(5),j1y(5):j2y(5)),7,0));

if(sz0==nx);
FFout=cat(2,FFout,...
      cat(1,sym_g_mod(FF{3}(1:nx,1:nx),5,0),FF{3}(1:nx,1:nx),sym_g_mod(FF{3}(1:nx,1:nx),7,0),sym_g_mod(FF{3}(1:nx,1:nx),6,0)));
elseif(sz0==(nx+1));
FFout=cat(2,FFout,...
      cat(1,sym_g_mod(FF{3}(1:nx,2:nx+1),5,0),FF{3}(1:nx,1:nx),sym_g_mod(FF{3}(2:nx+1,1:nx),7,0),sym_g_mod(FF{3}(2:nx+1,2:nx+1),6,0)));
end;

FFout=cat(1,FFout(4*nx-N+1:4*nx,:),FFout,FFout(1:N,:));
%keyboard
%treat face3 separately:
clear F3
if(sz0==(nx+1));
  F3=cat(1,sym_g_mod(FF{1}(2:nx+1,3*nx-N+1:3*nx),7,0),FF{3}(1:nx,1:nx),FF{4}(1:N,1:nx));
  F3=cat(2,F3,cat(1,zeros(N,N),sym_g_mod(FF{5}(1:N,2:nx+1),5,0),zeros(N,N)));
  F3=F3(:,1:nx+N);
  F3(nx+N+1:nx+2*N,nx+1:nx+N)=FF{5}(1:N,1:N);
  tmp=sym_g_mod(FF{1},6,0);F3(1:N,nx+1:nx+N)=tmp(nx-N+1:nx,1:N);
else;
  F3=cat(1,sym_g_mod(FF{1}(1:nx+0,3*nx-N+1:3*nx),7,0),FF{3}(1:nx,1:nx),FF{4}(1:N,1:nx));
  F3=cat(2,F3,cat(1,zeros(N,N),sym_g_mod(FF{5}(1:N,1:nx),5,0),zeros(N,N)));
  F3=F3(:,1:nx+N);
  F3(nx+N+1:nx+2*N,nx+1:nx+N)=FF{5}(1:N,1:N);
  tmp=sym_g_mod(FF{1},6,0);F3(1:N,nx+1:nx+N)=tmp(nx-N+1:nx,1:N);
end;

%size(F3): 108x99 for XG, 108x99 for bathy, 108x99 for mask
%size(FFout): 378 x 360 for XG, 378x360 for bathy, 378x360 for mask

%now resampling:
sz=size(FFout);
sz3=size(F3);
i=1; i1=(i-1)*N+1;i2=i*N; ic1=(i2-i1)/2+1;         %needs to be an integer

clear FFrN FF3rN;
if(flagInterp==0);
  FFrN=FFout(ic1:N:end-ic1+1,ic1:N:end-ic1+1);	%42x40 for XG
  if(sz0==nx);
    FFrN=FFrN(2:end-1,:);					%40x40 for mask1
  elseif(sz0==(nx+1));
    FFrN=FFrN(2:end,:);						%41x40 for XG
  end;
  FF3rN=F3(ic1:N:end-ic1+1,ic1:N:end-ic1+1);		%12x11 for all
  FF3rN=FF3rN(2:end,:);						%11x11 for XG
  if(sz0==nx);
    FF3rN=FF3rN(1:end-1,1:end-1);					%10x10 for mask
  end;
else;
  if(sz0==(nx+1));
    warning('note that for points at grid edge should NOT be interpolating');
  end;
  for i=1:sz(1)/N;
    i1=(i-1)*N+1;i2=i*N;
    for j=1:sz(2)/N;
      j1=(j-1)*N+1;j2=j*N;
      FFrN(i,j)=mean(mean(FFout(i1:i2,j1:j2)));
    end;
  end; %42x40	for bathy
  FFrN=FFrN(2:end-1,:);			%40x40 for bathy

  for i=1:sz3(1)/N;
    i1=(i-1)*N+1;i2=i*N;
    for j=1:sz3(2)/N;
      j1=(j-1)*N+1;j2=j*N;
      FF3rN(i,j)=mean(mean(F3(i1:i2,j1:j2)));
    end;
  end;%12x11 for bathy
  FF3rN=FF3rN(2:end-1,1:end-1);	%10x10 for bathy

end;

%now put back into 5 faces:
nx1=nx/N;
if(sz0==(nx+1));
  FFrNf{1}=FFrN(1:(nx1+1),1:(3*nx1+1));
  FFrNf{2}=FFrN(nx1+1:(2*nx1+1),1:(3*nx1+1));
  FFrNf{4}=sym_g_mod(FFrN(2*nx1+1:3*nx1+1,1:3*nx1+1),5,0);
  FFrNf{5}=sym_g_mod(FFrN(3*nx1+1:4*nx1+1,1:3*nx1+1),5,0);

  FFrNf{3}=FF3rN;

else;
  FFrNf{1}=FFrN(1:nx1,1:3*nx1);
  FFrNf{2}=FFrN(nx1+1:2*nx1,1:3*nx1);
  FFrNf{4}=sym_g_mod(FFrN(2*nx1+1:3*nx1,1:3*nx1),5,0);
  FFrNf{5}=sym_g_mod(FFrN(3*nx1+1:4*nx1,1:3*nx1),5,0);

  FFrNf{3}=FF3rN;
end;

return

