      subroutine annet_posdata

!!    ~ ~ ~ PURPOSE ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!    this subroutine reads the output data created by the annet
!!    model and inputs it to the SWAT model
!!    Author: Arthur Hrast Essenfelder
!!    E-mail: arthur.essenfelder@unive.it
!!
!!    Version: 1.0
!!    Last update on: 07/11/2016
!!
!!    ~ ~ ~ INCOMING VARIABLES  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!    name             |units         |definition
!!    ----------------------------------------------------------
!!    inum1            |none          |subbasin id number
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!
!!    ~ ~ ~ OUTGOING VARIABLES  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!    name             |units         |definition
!!    ----------------------------------------------------------
!!    vol_annet(:)     |m^3/day       |calculated annet flow in for the day
!!    arrData(1)       |m^3/s         |calculated annet flow in (cms)
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!
!!    ~ ~ ~ LOCAL DEFINITIONS ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!    name             |units         |definition
!!    ----------------------------------------------------------
!!    ii               |none          |generic counter
!!    kk               |none          |generic counter
!!    arrSize          |none          |integer to store the size of the data array
!!    checkComma       |none          |counter to check the number of commas in the csv file line
!!    csvLine          |none          |string to store the csv data
!!    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
!!
!!    ~ ~ ~ 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
      character(2024) :: csvLine
      logical :: filecheck
      integer :: IOstatus
      integer :: ii, kk, icl, arrSize, checkComma
      real, allocatable :: arrData(:), arrPart(:)
      !! ---------------------------------------------------------------

      !! Preparaing the name of the output files -----------------------
      filename1 = "ySim.csv"
      filename2 = "ySim_db.csv"
      !! ---------------------------------------------------------------

      !! Reading the data from the .csv file ---------------------------
      !! Checking if file exists
      inquire(file=filename1, exist=filecheck)
      if (filecheck) then
        open(115, file=filename1, action='read', status='old')
        read(115, *)
        read(115, '(a2024)') csvLine
        close(115)
      else
        csvLine = "0"
      end if
      csvLine = trim(csvLine)
      !! ---------------------------------------------------------------

      !! Counting the number of variables ------------------------------
      arrSize = 1
      checkComma = 0
      do
        if (index(csvLine(checkComma:len(csvLine)), ',') == 0) then
            exit
        end if
        checkComma = checkComma + &
                     index(csvLine(checkComma:len(csvLine)), ',')
        arrSize = arrSize + 1
      end do
      allocate(arrData(arrSize))
      !! ---------------------------------------------------------------

      !! Getting the data values ---------------------------------------
      !! Checking if file exists
      inquire(file=filename1, exist=filecheck)
      if (filecheck) then
        open(115, file=filename1, action='read', status='old')
        read(115, *)
        read(115, *) arrData
        close(115)
      else
        arrData = 0.
      end if
      !! ---------------------------------------------------------------

      !! Writting results to file --------------------------------------
      !! Checking if file exists, otherwise create it
      inquire(file=filename2, exist=filecheck)
      if (filecheck) then
        open(9980, file=filename2, action='write', status='old', &
             access='append')
      else
        open(9980, file=filename2, action='write', status='replace')
        write(9980, 1001)
      end if
      !! Write the data to the file
      do kk=1,msub
        write(9980, 1004, advance = 'no') kk
        write(9980, 1003, advance = 'no') ","
        write(9980, 1004, advance = 'no') i_mo
        write(9980, 1003, advance = 'no') ","
        write(9980, 1004, advance = 'no') icl(iida)
        write(9980, 1003, advance = 'no') ","
        write(9980, 1004, advance = 'no') iyr
        write(9980, 1003, advance = 'no') ","
        do ii=1,arrSize
            write(9980, 1002, advance = 'no') vol_annet(ii,kk)
            if (ii /= arrSize) then
                write(9980, 1003, advance = 'no') ","
            end if
        end do
        write(9980, *)
      end do
      close(9980)
      !! ---------------------------------------------------------------

1001  format ("RCH,MO,DA,YR,FLOW")
1002  format (f10.4)
1003  format (a1)
1004  format (i4)

      return
      end
