Water mask resizing, Fortran
module ar_stats
implicit none
integer scf
real, allocatable, dimension(:,:) :: wm
real, allocatable, dimension(:,:) :: new_wm
contains
subroutine mean
! This subroutine runs a moving window across large array
! and does simple averaging
integer xdim, ydim
integer i, j
integer , dimension(2) :: sh
integer , allocatable, dimension(:,:) :: box
allocate(box(scf,scf))
if ((allocated(wm)).and.(allocated(new_wm))) then
sh = shape(wm)
xdim = sh(1)
ydim = sh(2)
! "scf + 1" is used to tackle difference in
! Python and Fortran array indexing approaches
do i = 1, xdim - scf + 1, scf
do j = 1, ydim - scf + 1, scf
new_wm(i/scf + 1, j/scf + 1) = sum(wm(i: i+scf-1, j: j+scf-1))/scf**2
end do
end do
end if
end subroutine mean
subroutine mode
! subroutine "mode" runs a moving window across large array
! flattens it to a 1D array and finds *single* most common
! value via counting elements occurrences. Can be easily extended
! to returning several modal values (if this is the case)
integer xdim, ydim
integer i, j
integer ndim
integer, dimension(2) :: sh
integer, allocatable, dimension(:,:) :: box ! moving window
real, allocatable, dimension(:) :: fbox ! flat box
real mfv ! mfv - most frequent value
integer ncounter, ocounter ! new counter and old counter
integer m, n
allocate(box(scf,scf))
allocate(fbox(scf**2))
if ((allocated(wm)).and.(allocated(new_wm))) then
sh = shape(wm)
xdim = sh(1)
ydim = sh(2)
ndim = scf * scf
do i = 1, xdim - scf + 1, scf
do j = 1, ydim - scf + 1, scf
mfv = -999
ocounter = 0
box = (wm(i: i+scf-1, j: j+scf-1))
fbox = reshape(box, (/ ndim /) )
do m = 1, ndim
ncounter = 1
do n = m, ndim
if (fbox(n).eq.fbox(m)) then
ncounter = ncounter + 1
end if
end do
if (ncounter.gt.ocounter) then
mfv = fbox(m)
ocounter = ncounter
end if
end do
new_wm(i/scf + 1, j/scf + 1) = mfv
end do
end do
end if
end subroutine mode
end module array_funcs