function wvelmass=calc_wvelmass(uvelmass,vvelmass,ggrid)
% 
%function wvelmass=calc_wvelmass(uvelmass,vvelmass,ggrid)
%
% Input: in compact format
%  [u,v]velmass,nfx,nfy
%  ggrid.[nfx,nfy,RAC,DXG,DYG,DRF,hFacC]
% Output: in compact format
%----

%27.nov.2019  {{{
%testing if Del dot u is zero
%from mitgcm: integrate_for_w.F, pass in k, uFld, vFld, rStarDhDt
%C for rigidLid=.false., select_rStar=0
%C--   Calculate velocity field "volume transports" through
%C     tracer cell faces (anelastic: scaled as a mass transport).
%      DO j=1,sNy+1
%        DO i=1,sNx+1
%          uTrans(i,j) = uFld(i,j,k,bi,bj)
%     &                *_dyG(i,j,bi,bj)*deepFacC(k)*rhoFacC(k)	!deepFacC(k) = 1. _d 0
%     &                *drF(k)*_hFacW(i,j,k,bi,bj)		!in set_grid_factors.F
%          vTrans(i,j) = vFld(i,j,k,bi,bj)
%     &                *_dxG(i,j,bi,bj)*deepFacC(k)*rhoFacC(k)	!rhoFacC(k) = 1. _d 0
%     &                *drF(k)*_hFacS(i,j,k,bi,bj)		!in set_ref_state.F
%        ENDDO
%      ENDDO
%      DO j=1,sNy
%        DO i=1,sNx
%          conv2d(i,j) = -( uTrans(i+1,j)-uTrans(i,j)
%     &                    +vTrans(i,j+1)-vTrans(i,j) )
%        ENDDO
%      ENDDO
%
%C-  o Free Surface case (r-Coordinate):
%C      non zero at surface ; zero under-ground and at r_lower boundary
%        IF (k.EQ.Nr) THEN
%          DO j=1,sNy
%           DO i=1,sNx
%             wFld(i,j,k,bi,bj) =
%     &          conv2d(i,j)*recip_rA(i,j,bi,bj)	!recip_deepFac2F(k) = 1. _d 0
%     &         *maskC(i,j,k,bi,bj)			!in set_grid_factors.F (k=[1,Nr+1])
%     &         *recip_deepFac2F(k)*recip_rhoFacF(k)	!recip_rhoFacF(k) = 1. _d 0
%           ENDDO					!in set_ref_state.F (k=[1,Nr+1])
%          ENDDO
%        ELSE
%          DO j=1,sNy
%           DO i=1,sNx
%             wFld(i,j,k,bi,bj) =
%     &        ( wFld(i,j,k+1,bi,bj)*deepFac2F(k+1)*rhoFacF(k+1)
%     &         +conv2d(i,j)*recip_rA(i,j,bi,bj)
%     &        )*maskC(i,j,k,bi,bj)
%     &         *recip_deepFac2F(k)*recip_rhoFacF(k)
%           ENDDO
%          ENDDO
%        ENDIF                   %}}} 

nx=ggrid.nx;ny=ggrid.ny;nz=ggrid.nz;
nfx=ggrid.nfx;nfy=ggrid.nfy;
tmp=ggrid.hFacC(:,:,1);
[~,~,mskC]=get_aste_mask_obcs(tmp,tmp,tmp,nx);

rac=ggrid.RAC;rac(find(rac==0))=nan;rac=get_aste_faces(rac,nfx,nfy);
hf=ggrid.hFacC;hf(find(hf>0))=1;	%note this is a binary [0,1]
hf=hf.*repmat(mskC,[1 1 nz]);

drf3d=ones(nx,ny,nz);for k=1:nz;drf3d(:,:,k)=drf3d(:,:,k).*ggrid.DRF(k);end;

tmpUo=drf3d.*repmat(ggrid.DYG,[1 1 nz]).*uvelmass;                    %3d, m3/s
tmpVo=drf3d.*repmat(ggrid.DXG,[1 1 nz]).*vvelmass;                    %3d, m3/s

hconv=calc_UV_conv_mod(tmpUo,tmpVo,nfx,nfy); %5 faces
hf=get_aste_faces(hf,nfx,nfy);               %5 faces
for jf=1:5;if(nfx(jf)>0 & nfy(jf)>0);hconv{jf}=hconv{jf}.*hf{jf};end;end;

%reconstruct wvelmass, will have to work from bottom up
clear wvelmass
for jf=1:5;
  %fprintf('%i ',jf);
  wvelmass{jf}=nan(size(hconv{jf}));
  if(nfx(jf)>0 & nfy(jf)>0);
    w_bot=zeros(nfx(jf),nfy(jf));
    for k=nz:-1:1;
      w_top=hconv{jf}(:,:,k)./rac{jf}-w_bot;
      wvelmass{jf}(:,:,k)=w_top;
      w_bot=-w_top;
      %fprintf('%i ',k);
    end;
  end;
  %fprintf('\n');
end;

if(nfx(2)>0&nfy(2)>0);
  wvelmass=cat(2,wvelmass{1},...
                 wvelmass{2},...
                 wvelmass{3},...
                 reshape(wvelmass{4},nfy(4),nfx(4),nz),...
                 reshape(wvelmass{5},nfy(5),nfx(5),nz));
else;
  wvelmass=cat(2,wvelmass{1},...
                 wvelmass{3},...
                 reshape(wvelmass{4},nfy(4),nfx(4),nz),...
                 reshape(wvelmass{5},nfy(5),nfx(5),nz));
end;

return
