      subroutine irrigation(irr_type, irr_scode, irr_hru)

!!    ~ ~ ~ PURPOSE ~ ~ ~
!!    this subroutine controls the irrigation process

!!    ~ ~ ~ INCOMING VARIABLES ~ ~ ~
!!    name           |units         |definition
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!    auto_wstr(:)   |none or mm    |water stress threshold that triggers irrigation
!!    irr_hru        |none          |HRU number
!!    irr_no(:)      |none          |irrigation source location (auto-irrigation)
!!                                  |if IRR=1, IRRNO is the number of the
!!                                  |          reach
!!                                  |if IRR=2, IRRNO is the number of the
!!                                  |          reservoir
!!                                  |if IRR=3, IRRNO is the number of the
!!                                  |          subbasin
!!                                  |if IRR=4, IRRNO is the number of the
!!                                  |          subbasin
!!                                  |if IRR=5, not used
!!    irr_sc(:)      |none          |irrigation source code (auto-irrigation):
!!                                  |1 divert water from reach
!!                                  |2 divert water from reservoir
!!                                  |3 divert water from shallow aquifer
!!                                  |4 divert water from deep aquifer
!!                                  |5 divert water from unlimited source
!!    irr_scode      |none          |irrigation source code (auto-irrigation):
!!																	|0 any of the below:
!!                                  |1 divert water from reach
!!                                  |2 divert water from reservoir
!!                                  |3 divert water from shallow aquifer
!!                                  |4 divert water from deep aquifer
!!                                  |5 divert water from unlimited source
!!    irr_noa(:)     |none          |irrigation source location (auto-irrigation)
!!                                  |if IRR=1, IRRNO is the number of the
!!                                  |          reach
!!                                  |if IRR=2, IRRNO is the number of the
!!                                  |          reservoir
!!                                  |if IRR=3, IRRNO is the number of the
!!                                  |          subbasin
!!                                  |if IRR=4, IRRNO is the number of the
!!                                  |          subbasin
!!                                  |if IRR=5, not used
!!    irr_sca(:)     |none          |irrigation source code (auto-irrigation):
!!                                  |1 divert water from reach
!!                                  |2 divert water from reservoir
!!                                  |3 divert water from shallow aquifer
!!                                  |4 divert water from deep aquifer
!!                                  |5 divert water from unlimited source
!!    irr_type       |none          |irrigation flag:
!!                                  |0 no irrigation operation on current day
!!                                  |1 scheduled irrigation
!!                                  |2 auto irrigation
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

!!    ~ ~ ~ OUTGOING VARIABLES ~ ~ ~
!!    name        |units         |definition
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!    irrno(:)    |none          |irrigation source location
!!                               |if IRR=1, IRRNO is the number of the
!!                               |          reach
!!                               |if IRR=2, IRRNO is the number of the
!!                               |          reservoir
!!                               |if IRR=3, IRRNO is the number of the
!!                               |          subbasin
!!                               |if IRR=4, IRRNO is the number of the
!!                               |          subbasin
!!                               |if IRR=5, not used
!!    irrsc(:)    |none          |irrigation source code:
!!                               |1 divert water from reach
!!                               |2 divert water from reservoir
!!                               |3 divert water from shallow aquifer
!!                               |4 divert water from deep aquifer
!!                               |5 divert water from unlimited source
!!    sol_sumfc(:)|mm H2O        |amount of water held in the soil profile
!!                               |at field capacity
!!    sol_sw(:)   |mm H2O        |amount of water stored in soil profile on any
!!                               |given day
!!    strsw(:)    |none          |fraction of potential plant growth achieved
!!                               |on the day where the reduction is caused by
!!                               |water stress
!!    wstrs_id(:) |none          |water stress identifier:
!!                               |1 plant water demand
!!                               |2 soil water deficit
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

!!    ~ ~ ~ LOCAL DEFINITIONS ~ ~ ~
!!    name        |units         |definition
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!    flag        |none          |irrigation flag:
!!                               |0 no irrigation operation on current day
!!                               |1 scheduled irrigation
!!                               |2 auto irrigation
!!    j           |none          |HRU number
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

!!    ~ ~ ~ SUBROUTINES/FUNCTIONS CALLED ~ ~ ~
!!    Intrinsic: 
!!    SWAT: irr_source

!!    ~ ~ ~ ~ ~ ~ END SPECIFICATIONS ~ ~ ~ ~ ~ ~

      use parm
      implicit none

      integer :: irr_type, irr_scode, irr_hru
      integer :: flag, j
      
!! Get current hru number
      j = 0
      j = irr_hru
      
!! Check if it is a scheduled or auto-irrigation operation
      flag = 0
      if (irr_flag(j) == 1) flag = 1   !! If scheduled irriation = 1
      if (irra_flag(j) == 1) flag = 2  !! If auto-irriation = 2
      if (flag == 0) return            !! If no flag is active, exit subroutine
      
!! Check if current irrigation type is the same as the one requested
      if (flag /= irr_type) return
      
!! Get irrigation source code and irrigation source location
			if (flag == 1) then      !! If scheduled irrigation
			  irrsc(j) = irr_sc(j)
        irrno(j) = irr_no(j)
			elseif (flag == 2) then  !! If auto-irrigation
			  irrsc(j) = irr_sca(j)
        irrno(j) = irr_noa(j)
			end if
			
!! Check if current HRU irrsc is the same as the one requested
      if (irr_scode /= 0 .and. irrsc(j) /= irr_scode) return
      
!! Proceed with irrigation operation
      if (flag == 1) then      !! Scheduled irrigation
        call irr_source(flag, j)
        irr_flag(j) = 0        !! Set the scheduled irrigation flag to 0 (i.e. irrigation event already applied)
      elseif (flag == 2) then  !! If auto-irrigation
   !! Check if auto-irrigation is triggered 
        if (((wstrs_id(j) == 1) .and. (strsw(j) < auto_wstr(j))) .or.     !!Plant water demand
     &   ((wstrs_id(j)==2).and.((sol_sumfc(j)-sol_sw(j))>auto_wstr(j))))  !!Soil water deficit
     &  then
          call irr_source(flag, j)
        end if
      end if

      return
      end
