function Fout=blend_field(Fin_1,Fin_2,ind_1,direction_blend,N_blend)
%------------
%function Fout=blend_field(Fin_1,Fin_2,ind_1,direction_blend,N_blend)
%
% Function to blend between Fin_1 and Fin_2, starting from ind_1 in
%  in the direction_blend (increasing (+1) or decreasing (-1) )
%  by N_blend grid points.
%
% Input: 
%   Fin_1           : [1 x L]
%   Fin_2           : [1 x L]
%   ind_1           : [1 x 1]
%   direction_blend : +1 for increasing, -1 for decreasing
%   N_blend         : blend over this many indices
%
% Output:
%   Fout:           : [1 x L]
%
% atn 06Feb2017
%--------------

%check size
sz1=size(Fin_1);
sz2=size(Fin_2);

%put into row
if(sz1(2)==1 & sz1(1)>sz1(2)); Fin_1=Fin_1';end;
if(sz2(2)==1 & sz2(1)>sz2(2)); Fin_2=Fin_2';end;

if(max(sz1)~=max(sz2));error('Fin_1 and Fin_2 have conflicting size');end;

ind_blend=ind_1:direction_blend:ind_1+direction_blend*(N_blend-1);

Fout=zeros(N_blend+2,size(Fin_1,2));

%first, just assume indices are increasing
Fout(1,:)=Fin_1;
Fout(N_blend+2,:)=Fin_2;

for i=1:N_blend;

    w_2 = i/N_blend;
    w_1 = 1-w_2;
    Fout(i+1,:)=w_1.*Fin_1+w_2.*Fin_2;
    %fprintf('%i %i %f %f\n',[i ind_blend(i) w_1 w_2]);
end;

if(direction_blend<0);
  Fout=flipud(Fout);
end;

%put Fout in the right orientation, to be consistent with F_in[1,2]
if(sz1(2)==1 & sz1(1)>sz1(2));
  Fout=Fout';
  if(direction_blend<0);
    Fout=fliplr(Fout);
  end;
end;

return
