      subroutine irrigate(flag, j, volmm)

!!    ~ ~ ~ PURPOSE ~ ~ ~
!!    this subroutine applies irrigation water to HRU

!!    ~ ~ ~ INCOMING VARIABLES ~ ~ ~
!!    name        |units         |definition
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!    aairr(:)    |mm H2O        |average annual amount of irrigation water
!!                               |applied to HRU that sums up to sol_sw
!!    alirr(:)    |mm H2O        |average annual amount of irrigation water
!!                               |applied to HRU that is lost due to
!!                               |application efficiency
!!    aqirr(:)    |mm H2O        |average annual amount of irrigation water
!!                               |applied to HRU that returns to runoff
!!    tird(:)     |mm H2O        |total average annual amount of irrigation
!!                               |water applied to HRU on current day
!!    curyr       |none          |current year of simulation
!!    irn(:)      |none          |average annual number of irrigation
!!                               |applications in HRU
!!    irrefm(:)   |none          |irrigation efficiency - schedule irrigation
!!    irr_eff(:)  |none          |irrigation efficiency - auto-irrigation
!!    irrsq(:)    |none          |fraction of total applied irrigation water
!!                               |that is lost as runoff - schedule irrigation
!!    irr_asq(:)  |none          |fraction of total applied irrigation water
!!                               |that is lost as runoff - auto-irrigation
!!    nyskip      |none          |number of years to skip output summarization
!!                               |and printing
!!    shallst(:)  |mm H2O        |depth of water in shallow aquifer
!!    sol_fc(:,:) |mm H2O        |amount of water available to plants in soil
!!                               |layer at field capacity (fc - wp)
!!    sol_nly(:)  |none          |number of soil layers
!!    sol_st(:,:) |mm H2O        |amount of water stored in the soil layer
!!                               |on any given day (less wp water)
!!    hrumono(22,:)|mm H2O       |amount of irrigation water applied to HRU
!!                               |during month
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

!!    ~ ~ ~ OUTGOING VARIABLES ~ ~ ~
!!    name        |units         |definition
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!    aairr(:)    |mm H2O        |average annual amount of irrigation water
!!                               |applied to HRU that sums up to sol_sw
!!    alirr(:)    |mm H2O        |average annual amount of irrigation water
!!                               |applied to HRU that is lost due to
!!                               |application efficiency
!!    aqirr(:)    |mm H2O        |average annual amount of irrigation water
!!                               |applied to HRU that returns to runoff
!!    tird(:)     |mm H2O        |total average annual amount of irrigation
!!                               |water applied to HRU on current day
!!    aird(:)     |mm H2O        |amount of irrigation water applied to
!!                               |HRU on current day that sums up to sol_sw
!!    lird(:)     |mm H2O        |amount of irrigation water applied to
!!                               |HRU on current day that is lost due to
!!                               |application efficiency
!!    qird(:)     |mm H2O        |amount of irrigation water applied to
!!                               |HRU on current day that returns to runoff
!!    tird(:)     |mm H2O        |total amount of irrigation water applied
!!                               |to HRU on current day
!!    irn(:)      |none          |average annual number of irrigation
!!                               |applications in HRU
!!    shallst(:)  |mm H2O        |depth of water in shallow aquifer
!!    sol_st(:,:) |mm H2O        |amount of water stored in the soil layer
!!                               |on any given day (less wp water)
!!    sol_sw(:)   |mm H2O        |amount of water stored in the soil profile
!!                               |on any given day
!!    sq_rto      |none          |fraction of total applied irrigation water
!!                               |that is lost as runoff
!!    hrumono(22,:)|mm H2O       |amount of irrigation water applied to HRU
!!                               |during month
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

!!    ~ ~ ~ LOCAL DEFINITIONS ~ ~ ~
!!    name        |units         |definition
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!    flag        |none          |irrigation flag:
!!                               |0 no irrigation operation on current day
!!                               |1 scheduled irrigation
!!                               |2 auto irrigation
!!    irreff      |none          |irrigation application efficiency
!!    k           |none          |HRU number
!!    volmm       |mm H2O        |depth irrigation water applied to HRU
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

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

      use parm
      implicit none

      integer, intent (in) :: j
      real, intent (in out) :: volmm
      integer :: flag
      real :: irreff

      irreff = 0.
      sq_rto = 0.

!! Check if it is a scheduled or auto-irrigation operation
      if (flag == 0) then
        return                          !! If no flag is active, exit subroutine
      end if

!! Get irrigation efficiency and runoff return parameters
      if (flag == 1) then      !! If scheduled irrigation
        irreff = irrefm(j)
        sq_rto = irrsq(j)
      elseif (flag == 2) then  !! If auto-irrigation
        irreff = irr_eff(j)
        sq_rto = irr_asq(j)
      end if

!! Calculate the amount of irrigation applied to soil and irrigation losses
      qird(j) = volmm * sq_rto
      aird(j) = volmm * irreff
      lird(j) = volmm - (aird(j) + qird(j))
      tird(j) = volmm

!! Check if lird is negative due to invalid irreff and sq_rto inputs (i.e. irreff + sq_rto > 1.0)
      if (lird(j) < 0.) then
        lird(j) = 0.
        qird(j) = volmm - aird(j)
      end if

!! Add the irrigation water losses to shallow aquifer storage
      shallst(j) = shallst(j) + lird(j)

!! Summary calculations
      if (curyr > nyskip) then
        irn(j) = irn(j) + 1
        aairr(j) = aairr(j) + aird(j)
        aqirr(j) = aqirr(j) + qird(j)
        alirr(j) = alirr(j) + lird(j)
        atirr(j) = atirr(j) + tird(j)
        hrumono(22,j) = hrumono(22,j) + tird(j)
      end if


      return
      end
