subroutine output_average_kp_matrix(config_label, nrepeats, stem, mean, se)
use util_mod, only: int_fmt, get_free_unit
integer, intent(in) :: config_label, nrepeats
character(10), intent(in) :: stem
real(dp), intent(in) :: mean(:, :), se(:, :)
integer :: irepeat
character(25) :: ind1, filename
integer :: i, j, k, temp_unit, repeat_ind
write(ind1, '(i15)') config_label
filename = trim(trim(stem)//'.'//trim(adjustl(ind1)))
temp_unit = get_free_unit()
open(temp_unit, file=trim(filename), status='replace')
! Write all the components of the various estimates of the matrix, above and including the
! diagonal, one after another on separate lines.
do i = 1, size(mean, 1)
do j = i, size(mean, 2)
! Write the index of the matrix element.
write(temp_unit, '('//int_fmt(i, 0)//', "1x" ,'//int_fmt(j, 0)//')', advance='no') i, j
if (nrepeats > 1) then
! Write the mean and standard error.
write(temp_unit, '(1x,es19.12,1x,a3,es19.12)') mean(i, j), "+/-", se(i, j)
else if (nrepeats == 1) then
! If we only have one sample then a standard error was not calculated, so
! only output the mean.
write(temp_unit, '(1x,es19.12)') mean(i, j)
end if
end do
end do
close(temp_unit)
end subroutine output_average_kp_matrix