subroutine avey_ibm(aver,var,ib,ie,jb,je,kb,ke,II,IIt)
implicit none
integer :: ib,ie,jb,je,kb,ke
real :: aver(ib:ie,kb:ke)
real :: avero(ib:ie,kb:ke)
real :: var(ib:ie,jb:je,kb:ke)
integer :: II(ib:ie,jb:je,kb:ke)
integer :: IIt(ib:ie,kb:ke)
logical :: lytdump,lnan
avero = 0.
aver = 0.
avero = sum(var(ib:ie,jb:je,kb:ke)*II(ib:ie,jb:je,kb:ke), DIM=2)
call MPI_ALLREDUCE(avero(ib:ie,kb:ke), aver(ib:ie,kb:ke), (ke-kb+1)*(ie-ib+1), MY_REAL,MPI_SUM, comm3d,mpierr)
where (IIt==0)
aver = -999.
elsewhere
aver = aver/IIt
endwhere
end subroutine avey_ibm