function [wei,ind]=get_z_weight(z1,z0,flag_topbottom)
%------
%function [wei,ind]=get_z_weight(z1,z0,flag_topbottom)
% 
% Goal:  obtain set of weights (wei) and index (ind)
%        to interpolate from depths z0 to z1
% Ex: z0 is rC of 50 vertical levels
%     z1 is rC of 90 vertical levels
%     and need to interpolate 3D fields from z0 grid to z1 grid
% Input: 
%    z1: e.g. rC of 90 vertical lev, will take abs within subroutine
%    z0: e.g. rC of 50 vertical lev, will take abs
%  flag: 0 will assign nan if z1 is out of bound of z0
%        1 will give weights [1 0] or [0 1] to top and bottom lev
%-------

z0=abs(squeeze(z0));z1=abs(squeeze(z1));
if(size(z0,1)>size(z0,2));z0=z0';end;	%put into [1 50]
if(size(z1,1)>size(z1,2));z1=z1';end;	%put into [1 90]

nz=length(z1);

for k=1:nz;
  iz=sort(closest(z1(k),z0,2));
  ss=prod(sign(z0(iz)-z1(k)));
  if(ss>0&sign(z0(iz(1))-z1(k))>0);         %above z0(1)
    w1=1;w2=0;
  elseif(ss>0&sign(z0(iz(1))-z1(k))<0);     %below z0(end)
    w1=0;w2=1;
  else;
    dz=z0(iz(2))-z0(iz(1));                 %positive
    dz1=z1(k)-z0(iz(1));w1=1-dz1/dz;
    dz2=z0(iz(1))-z1(k);w2=1-w1;
  end;
  wei(k,:)=[w1 w2];
  ind(k,:)=[iz(1) iz(2)];
end;

return
