subroutine management_write_user_output(mb, ndayssim)
use output, only : output_open_file
integer, intent(in) :: mb
integer, intent(in) :: nDaysSim
! this subroutine is called by mainpro at the end of the simulation
! it writes one file for each water user and one file for each subbasin involved in transfers
integer :: i, j, funit
character(len=path_max_length) :: fname
TYPE (TWaterUser), POINTER :: pWU
TYPE (TSubbasin), POINTER :: pS
!--------------------------------------------------------
! write water user output file(s)
!--------------------------------------------------------
do i = 1, wam_nWU
fname = "wam_WU_"//trim(TWU(i)%name)//".csv"
pWU => TWU(i) ! pointer on current water user
funit = output_open_file(trim(fname))
! if water user is a destination (subbasin or external destination) that receives water from a subbasin
if (pWU % wu_opt <= 2) then ! a water user providing water to another subbasin or external destination
write(funit, fmt='(A43)')"Year, Day, Demand_m3s, Supplied_m3s, Losses_m3s"
do j = 1, nDaysSim
write(funit, fmt='(i4, ", ", i3, 3(", ", f10.2))') wam_y(j), wam_d(j), pWU%data(j), pWU%supplied(j), pWU%tr_losses(j)
end do
close(funit)
end if
! if water user receives water from an external source
if (pWU % wu_opt == 3) then ! a water user receiving water from an external source
write(funit, fmt='(A32)')"Year, Day, Supplied_m3s, Losses_m3s"
do j = 1, nDaysSim
write(funit, fmt='(i4, ", ", i3, 2(", ", f10.2))') wam_y(j), wam_d(j), pWU%supplied(j), pWU%tr_losses(j)
end do
close(funit)
end if
! if water user is irrigation
if (pWU % wu_opt == 4) then ! a water user providing water to another subbasin or external destination
! if ( pWU%subs == 0 ) pS => wam_get_pointer_subbasin(pWU%subd)
! if ( pWU%subs > 0 ) pS => wam_get_pointer_subbasin(pWU%subs)
write(funit, fmt='(A83)')"Year, Day, PlantDemand_mm, PlantDemand_m3s, totIrrDemand_m3s, Supplied_m3s, tr_Losses_m3s"
do j = 1, nDaysSim
write(funit, fmt='(i4, ", ", i3, 5(", ", f13.6))') wam_y(j), wam_d(j), (pWU%plantDemand(j)*86400./pWU%area*1000.), &
pWU % plantDemand(j), pWU % irrigationDemand(j), pWU % supplied(j), pWU % tr_losses(j)
end do
close(funit)
end if
end do ! i = 1, wam_nWU
!--------------------------------------------------------
!--------------------------------------------------------
! write subbasin output file(s)
!--------------------------------------------------------
do i = 1, mb
if (wamTSub(i) % subnum > 0 ) then
pS => wamTSub(i) % pSub
!fname = "transSUB"//
!fname = gen_filename(pS%subnr, "transSUB", ".out", len_trim("transSUB"))
fname = gen_filename(pS%subnr, "wam_SUB", ".csv", len_trim("wam_SUB"))
funit = output_open_file(trim(fname))
write(funit, fmt='(A99)') &
"Year, Day, Q_act_m3s, Q_Min_m3s, extInflow_m3s, lossesIn_m3s, totDemand_m3s, withdrawal_m3s, losses_out_m3s"
do j = 1, nDaysSim
write(funit, fmt='(i4, ", ", i3, 7(", ", f12.2))') wam_y(j), wam_d(j), pS%Q_act(j), pS%Q_min(j), &
pS % inflow(j), pS % tr_losses_in(j), pS % totDemand(j), pS % withdrawal(j), pS % tr_losses_out(j)
end do
close(funit)
end if
end do
!--------------------------------------------------------
! write irrigation summary output file
!--------------------------------------------------------
funit = output_open_file("wam_irrigation_summary.csv")
write(funit, fmt='(A58)')"Year, Day, plantDemand_m3s, irrigationDemand_m3s, supplied_m3s"
do j = 1, nDaysSim
write(funit, fmt='(i4, ", ", i3, 3(", ", f12.2))') &
wam_y(j), wam_d(j), wam_plantDemand_summary(j), &
wam_irrigationDemand_summary(j), wam_supplied_summary(j)
end do
close(funit)
end subroutine management_write_user_output