subroutine terminate
use input, only : &
#ifdef with_netcdf
input_nc_close, &
#endif
get_config_fid
use management, only : bWAM_Module, management_write_user_output
use subbasin, only : mb
use time, only : nDaysSim
use catchment, only : dealloc_catchment
use crop, only : crop_deallocate
use erosion, only : dealloc_erosion
use evapotranspiration, only : dealloc_evapotranspiration
use groundwater, only : dealloc_groundwater
use hydrotope, only : dealloc_hydrotope
use nutrient, only : dealloc_nutrient
use output, only : dealloc_output
use reservoir, only : dealloc_reservoir
use river, only : dealloc_river
use snow, only : dealloc_snow
use soil, only : dealloc_soil
use subbasin, only : subbasin_close
use vegetation, only : dealloc_vegetation
if (bWAM_Module) call management_write_user_output(mb, ndayssim)
close(get_config_fid())
call dealloc_catchment
call crop_deallocate
call dealloc_erosion
call dealloc_evapotranspiration
call dealloc_groundwater
call dealloc_hydrotope
call dealloc_nutrient
call dealloc_output
call dealloc_reservoir
call dealloc_river
call dealloc_snow
call dealloc_soil
call subbasin_close
call dealloc_vegetation
!**** Close netcdf files
#ifdef with_netcdf
call input_nc_close
#endif
return
end subroutine terminate