subroutine spatial_avg_xy(aver,var,kb,ke,II,IIs,lnan)
implicit none
real , intent(out) :: aver(:)
real , intent(in) :: var(:,:,:)
integer, intent(in) :: kb, ke
integer, intent(in) :: II(:,:,:)
integer, intent(in) :: IIs(:)
logical, intent(in) :: lnan
integer, allocatable :: IId(:)
real, allocatable :: averl(:)
real, allocatable :: avers(:)
integer :: k, nk
nk = size(aver)
allocate(averl(kb:nk), avers(kb:nk), IId(kb:nk))
averl = 0.
avers = 0.
IId = IIs
do k = kb, nk
averl(k) = sum(var(:,:,k)*II(:,:,k))
enddo
! tg3315 22.03.19 - if not calculating stats and all blocks on lowest layer...
! should not be necessary but value at kb is used in modthermo so reasonable value must
! be assigned. Potentially should leave as before and only account for in modthermo...
if ((.not. lnan) .and. (IId(kb)==0)) then
averl(kb) = sum(var(:,:,kb))
IId(kb) = IId(ke)
end if
call MPI_ALLREDUCE(averl, avers, nk, MY_REAL, MPI_SUM, comm3d, mpierr)
where (IId==0)
aver = -999.
elsewhere
aver = avers/IId
endwhere
deallocate(averl, avers, IId)
end subroutine spatial_avg_xy