module RvicHistFlds

!-----------------------------------------------------------------------
! !DESCRIPTION:
! Module containing initialization of RVIC history fields and files
! This is the module that the user must modify in order to add new
! history fields or modify defaults associated with existing history
! fields.
!
! !USES:
  use shr_kind_mod     , only: r8 => shr_kind_r8
  use RvicVar          , only : runoff
  use RvicHistFile     , only : RvicHistAddfld, RvicHistPrintflds
  use rvic_cpl_indices , only : nt_rvic, rvic_tracers  

  implicit none
!
! !PUBLIC MEMBER FUNCTIONS:
  public :: RvicHistFldsInit 
  public :: RvicHistFldsSet
!
!
! REVISION HISTORY:
! Modified by Joe Hamman for use with RVIC
!------------------------------------------------------------------------

contains

!-----------------------------------------------------------------------

  subroutine RvicHistFldsInit()

    !-------------------------------------------------------
    ! DESCRIPTION:
    ! Build master field list of all possible fields in a history file.
    ! Each field has associated with it a ``long\_name'' netcdf
    ! attribute that
    ! describes what the field is, and a ``units'' attribute. A
    ! subroutine is
    ! called to add each field to the masterlist.
    !
    ! ARGUMENTS:
    implicit none
    !-------------------------------------------------------

    call RvicHistAddfld (fname='QCHANR', units='kg m-2 s-1',  &
         avgflag='A', long_name='RVIC river flow: '//trim(rvic_tracers(1)), &
         ptr_rof=runoff%runofflnd_nt1)

    call RvicHistAddfld (fname='QCHANR'//'_'//trim(rvic_tracers(2)), units='kg m-2 s-1', &
         avgflag='A', long_name='RVIC river flow: '//trim(rvic_tracers(2)), &
         ptr_rof=runoff%runofflnd_nt2)

    call RvicHistAddfld (fname='QCHOCNR', units='kg m-2 s-1', &
         avgflag='A', long_name='RVIC river discharge into ocean: '//trim(rvic_tracers(1)), &
         ptr_rof=runoff%runoffocn_nt1)

    call RvicHistAddfld (fname='QCHOCNR'//'_'//trim(rvic_tracers(2)), units='kg m-2 s-1', &
         avgflag='A', long_name='RVIC river discharge into ocean: '//trim(rvic_tracers(2)), &
         ptr_rof=runoff%runoffocn_nt2)

    call RvicHistAddfld (fname='VOLR', units='kg m-2 s-1',  &
         avgflag='A', long_name='RVIC storage: '//trim(rvic_tracers(1)), &
         ptr_rof=runoff%volr_nt1)

    call RvicHistAddfld (fname='VOLR'//'_'//trim(rvic_tracers(2)), units='kg m-2 s-1',  &
         avgflag='A', long_name='RVIC storage: '//trim(rvic_tracers(2)), &
         ptr_rof=runoff%volr_nt2, default='inactive')

    call RvicHistAddfld (fname='DVOLRDT_LND', units='mm/s',  &
         avgflag='A', long_name='RVIC land change in storage: '//trim(rvic_tracers(1)), &
         ptr_rof=runoff%dvolrdtlnd_nt1, default='inactive')

    call RvicHistAddfld (fname='DVOLRDT_LND'//'_'//trim(rvic_tracers(2)), units='mm/s',  &
         avgflag='A', long_name='RVIC land change in storage: '//trim(rvic_tracers(2)), &
         ptr_rof=runoff%dvolrdtlnd_nt2, default='inactive')

    call RvicHistAddfld (fname='DVOLRDT_OCN', units='mm/s',  &
         avgflag='A', long_name='RVIC ocean change of storage: '//trim(rvic_tracers(1)), &
         ptr_rof=runoff%dvolrdtocn_nt1, default='inactive')

    call RvicHistAddfld (fname='DVOLRDT_OCN'//'_'//trim(rvic_tracers(2)), units='mm/s',  &
         avgflag='A', long_name='RVIC ocean change of storage: '//trim(rvic_tracers(2)), &
         ptr_rof=runoff%dvolrdtocn_nt2, default='inactive')

    ! Print masterlist of history fields
    call RvicHistPrintflds()

  end subroutine RvicHistFldsInit

!-----------------------------------------------------------------------

  subroutine RvicHistFldsSet()

    !-----------------------------------------------------------------------
    ! !DESCRIPTION:
    ! Set rvic history fields as 1d poitner arrays
    !
    implicit none
    !-----------------------------------------------------------------------

    ! Currently only have two tracers
    runoff%runofflnd_nt1(:)  = runoff%runofflnd(:,1)
    runoff%runofflnd_nt2(:)  = runoff%runofflnd(:,2)

    runoff%runoffocn_nt1(:)  = runoff%Runoff(:,1)
    runoff%runoffocn_nt2(:)  = runoff%runoffocn(:,2)

    runoff%dvolrdtlnd_nt1(:) = runoff%dvolrdtlnd(:,1)
    runoff%dvolrdtlnd_nt2(:) = runoff%dvolrdtlnd(:,2)

    runoff%dvolrdtocn_nt1(:) = runoff%dvolrdtocn(:,1)
    runoff%dvolrdtocn_nt2(:) = runoff%dvolrdtocn(:,2)

    runoff%volr_nt1(:)       = runoff%volrlnd(:,1)
    runoff%volr_nt2(:)       = runoff%volrlnd(:,2)

  end subroutine RvicHistFldsSet


end module RvicHistFlds

