      subroutine annet_predata

!!    ~ ~ ~ PURPOSE ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!    this subroutine prepares the input data to be read by the annet
!!    model
!!    Author: Arthur Hrast Essenfelder
!!    E-mail: arthur.essenfelder@unive.it
!!
!!    Version: 1.0
!!    Last update on: 04/03/2020
!!
!!    ~ ~ ~ INCOMING VARIABLES  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!    name           |units         |definition
!!    ----------------------------------------------------------
!!    i              |none          |day of the year, integer
!!    inum1          |none          |subbasin id number
!!    hru1           |none          |HRU id number
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!
!!    ~ ~ ~ OUTGOING VARIABLES  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!    name           |units         |definition
!!    ----------------------------------------------------------
!!    arrXsim(1)     |mmH2O         |mean pcp in watershed (mmH2O)
!!    arrXsim(2)     |degC          |mean max temperature in watershed (degC)
!!    arrXsim(3)     |degC          |mean min temperature in watershed (degC)
!!    arrXsim(4)     |mmH2O         |mean actual evap in watershed (mmH2O)
!!    arrXsim(5)     |mmH2O         |mean soil water content in watershed (mmH2O)
!!    arrXsim(6)     |mmH2O         |total irrigation water applied (mmH2O)
!!    arrXsim(7)     |m^3/s         |flow out of reach on day (m^3/s)
!!    arrXsim(8)     |m^3/s         |flow into reach on day (m^3/s)
!!    arrXsim(9)     |mmH2O         |groundwater flow in subbasin (mmH2O)
!!    arrXsim(10)    |boolean       |crop growing season
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!
!!    ~ ~ ~ LOCAL DEFINITIONS ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!    name           |units         |definition
!!    ----------------------------------------------------------
!!    ii             |none          |generic counter
!!    arrSize        |none          |integer to store the size of the data array
!!    iseason        |none          |integer to store the results of funseason
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!
!!    ~ ~ ~ SUBROUTINES/FUNCTIONS CALLED  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!    name           |type          |definition
!!    ----------------------------------------------------------
!!    funseason()    |function      |function to calculate the season of the year:
!!                                  |0 = error
!!                                  |1 = winter
!!                                  |2 = spring
!!                                  |3 = summer
!!                                  |4 = autumn
!!
!!    ~ ~ ~ ~ ~ ~ END SPECIFICATIONS  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

      !! Initial declarations ------------------------------------------
      use parm
      implicit none
      !! ---------------------------------------------------------------

      !! Declaring Variables -------------------------------------------
      character(16) :: filename1,filename2
      character(4) :: subcode
      logical :: filecheck
      integer :: ii, kk, icl, arrSize = 10
      integer :: funseason, iseason, IOstatus
      real, allocatable :: arrXsim(:)
      real :: cnv
      !! ---------------------------------------------------------------

      !! Preparing the inputs for the array data -----------------------
      !! Initialising the arrXsim
      allocate(arrXsim(arrSize))
      arrXsim = 0.

      !! Loading the data to the arrXsim
      !! Watershed scale variables
      arrXsim(1) = wshddayo(1)         !!mean pcp in watershed (mmH2O)
      arrXsim(2) = wshddayo(8)         !!mean max temperature in watershed (degC)
      arrXsim(3) = wshddayo(9)         !!mean min temperature in watershed (degC)
      arrXsim(4) = wshddayo(7)         !!mean actual evap in watershed (mmH2O)
      arrXsim(5) = wshddayo(35)        !!mean soil water content in watershed (mmH2O)
      
      !! Irrigation water applied
      kk = 0    !!Counter for irrigated HRUs
      do ii=1, nhru
        if ((aird(ii)+qird(ii))>0.) then
            arrXsim(6) = arrXsim(6) + aird(ii)+qird(ii)
            kk = kk + 1
        endif
      end do
      if (kk > 0) then
        arrXsim(6) = arrXsim(6)/kk     !!total irrigation water applied (mmH2O)
      endif
      
      !! Subbasin scale variables
      arrXsim(7) = rchdy(1,inum1)      !!flow out of reach on day (m^3/s)
      arrXsim(8) = rchdy(2,inum1)      !!flow into reach on day (m^3/s)
      arrXsim(9) = sub_gwq(inum1)      !!groundwater flow in subbasin (mmH2O)

      !! Temporal variables
      iseason = funseason(i)
      select case (iseason)
        case (1) !! winter
            arrXsim(10) = 0.
        case (2) !! spring
            arrXsim(10) = 1.
        case (3) !! summer
            arrXsim(10) = 1.
        case (4) !! autumn
            arrXsim(10) = 0.
        case default
            arrXsim(10) = 0.
      end select
      !! ---------------------------------------------------------------

      !! Preparaing the name of the output files -----------------------
      filename1 = "Xsim.csv"
      filename2 = "Xsim_db.csv"
      !! ---------------------------------------------------------------

      !! Writing the data to .csv file ---------------------------------
      !! Checking if file exists, otherwise create it (Xsim)
      open(9970, file=filename1, action='write', status='replace')
      write(9970, 1001)
      !! Write the data to the file
      write(9970, 1004, advance = 'no') inum1
      write(9970, 1003, advance = 'no') ","
      write(9970, 1004, advance = 'no') i_mo
      write(9970, 1003, advance = 'no') ","
      write(9970, 1004, advance = 'no') icl(iida)
      write(9970, 1003, advance = 'no') ","
      write(9970, 1004, advance = 'no') iyr
      write(9970, 1003, advance = 'no') ","
      do ii=1,arrSize
        write(9970, 1002, advance = 'no') arrXsim(ii)
        if (ii /= arrSize) then
            write (9970, 1003, advance = 'no') ","
        end if
      end do
      write(9970, *)
      close(9970)
      !! ---------------------------------------------------------------

      !! Writing the data to .csv database file ------------------------
      !! Checking if file exists, otherwise create it (Xsim_db)
      inquire(file=filename2, exist=filecheck)
      if (filecheck) then
        open(9971, file=filename2, action='write', status='old', &
             access='append')
      else
        open(9971, file=filename2, action='write', status='replace')
        write(9971, 1001)
      end if
      !! Write the data to the file
      write(9971, 1004, advance = 'no') inum1
      write(9971, 1003, advance = 'no') ","
      write(9971, 1004, advance = 'no') i_mo
      write(9971, 1003, advance = 'no') ","
      write(9971, 1004, advance = 'no') icl(iida)
      write(9971, 1003, advance = 'no') ","
      write(9971, 1004, advance = 'no') iyr
      write(9971, 1003, advance = 'no') ","
      do ii=1,arrSize
        write(9971, 1002, advance = 'no') arrXsim(ii)
        if (ii /= arrSize) then
            write (9971, 1003, advance = 'no') ","
        end if
      end do
      write(9971, *)
      close(9971)
      !! ---------------------------------------------------------------

1001  format ("RCH,MO,DA,YR,PCP,TMPmax,TMPmin,AET,SWC,IRR,&
              DFLOW,UFLOW,GWQ,CGS")
1002  format (f10.4)
1003  format (a1)
1004  format (i4)

      return
      end
