subroutine output_month(year, month)
! Take monthly averages and write to csv/bin, if interval is monthly.
integer, intent(in) :: year, month
integer :: sti(3, 3), stb(3, 3)
integer i, nd
! Format month label for csv output
write(output_month_ix(output_store_month_counter), "(i0.4, '-', i0.2)") year, month
! Take average of monthly values as needed
nd = output_ndays_per_month
do i = 1, output_nvars
! 0/1 (space, time) for current variable
stb = output_is_requested(i, :, :)
! indeces in aggregation arrays of current variable
sti = sum(output_is_requested(:i, :, :), dim=1)
if (output_var_state(i)) then
if (stb(1, 2) == 1) output_agg_hydrotope_monthly(sti(1, 2), :, :) = &
output_agg_hydrotope_monthly(sti(1, 2), :, :) / nd
if (stb(2, 2) == 1) output_agg_subbasin_monthly(sti(2, 2), :, :) = &
output_agg_subbasin_monthly(sti(2, 2), :, :) / nd
if (stb(3, 2) == 1) output_agg_catchment_monthly(sti(3, 2), :, :) = &
output_agg_catchment_monthly(sti(3, 2), :, :) / nd
end if
end do
! Increase/reset counters and write out if necessary
if (output_write_interval == "D") then
call output_write_monthly
else if (output_write_interval == "M") then
call output_write_daily
call output_write_monthly
output_store_day_counter = 1
else if (output_write_interval == "Y") then
output_store_month_counter = output_store_month_counter + 1
end if
output_ndays_per_year = output_ndays_per_year + output_ndays_per_month
output_ndays_per_month = 0
end subroutine output_month