! $Id: key_access.f90,v 1.7 2006-01-13 18:04:37 Rob_Andre Exp $
#include "fpreproc/byte_declare.h"

module key_access

  interface ka_init
     ! initialize or re-initialize table
     subroutine key2_init
     end subroutine key2_init
  end interface

  interface ka_free
     ! clear table
     subroutine key2_free
     end subroutine key2_free
  end interface

  interface ka_set
     ! set 
     module procedure key2double_set, key2int_set, key2bool_set
     module procedure key2doublearray_set, key2intarray_set, key2boolarray_set
  end interface

  interface ka_get
     ! get 
     module procedure key2double_get, key2int_get, key2bool_get
     module procedure key2doublearray_get, key2intarray_get, key2boolarray_get
  end interface

  interface ka_print
     subroutine key2print
     end subroutine key2print
  end interface

  interface ka_write_namelist
     module procedure key2write_namelist
  end interface

contains
  subroutine key2double_set(var_name, val)
    character*(*), intent(in) :: var_name  ! name of variable
    real*8,        intent(in) :: val       ! value

    byte_declare :: cname(len(var_name)+1) ! name for C

    call cstring(var_name, cname, '2C')
    call c_key2double_set(cname, val)
  end subroutine key2double_set

  subroutine key2doublearray_set(var_name, val)
    character*(*), intent(in) :: var_name  ! name of variable
    real*8,        intent(in) :: val(:)    ! value

    byte_declare :: cname(len(var_name)+1) ! name for C

    call cstring(var_name, cname, '2C')
    call c_key2doublearray_set(cname, val, size(val))
  end subroutine key2doublearray_set

  subroutine key2int_set(var_name, val)
    character*(*), intent(in) :: var_name  ! name of variable
    integer,       intent(in) :: val       ! value

    byte_declare :: cname(len(var_name)+1) ! name for C

    call cstring(var_name, cname, '2C')
    call c_key2int_set(cname, val)
  end subroutine key2int_set

  subroutine key2intarray_set(var_name, val)
    character*(*), intent(in) :: var_name  ! name of variable
    integer,       intent(in) :: val(:)    ! value

    byte_declare :: cname(len(var_name)+1) ! name for C

    call cstring(var_name, cname, '2C')
    call c_key2intarray_set(cname, val, size(val))
  end subroutine key2intarray_set

  subroutine key2bool_set(var_name, val)
    character*(*), intent(in) :: var_name  ! name of variable
    logical,       intent(in) :: val       ! value

    byte_declare :: cname(len(var_name)+1) ! name for C
    integer :: ival

    ival=0
    if (val) ival=1

    call cstring(var_name, cname, '2C')
    call c_key2bool_set(cname, ival)
  end subroutine key2bool_set

  subroutine key2boolarray_set(var_name, val)
    character*(*), intent(in) :: var_name  ! name of variable
    logical,       intent(in) :: val(:)    ! value

    byte_declare :: cname(len(var_name)+1) ! name for C
    integer :: ival(size(val))

    ival=0
    where (val)
       ival=1
    elsewhere
       ival=0
    end where

    call cstring(var_name, cname, '2C')
    call c_key2boolarray_set(cname, ival, size(val))
  end subroutine key2boolarray_set

  subroutine key2double_get(var_name, val)
    character*(*), intent(in)  :: var_name  ! name of variable
    real*8,        intent(out) :: val       ! value

    byte_declare :: cname(len(var_name)+1) ! name for C

    call cstring(var_name, cname, '2C')
    call c_key2double_get(cname, val)
  end subroutine key2double_get

  subroutine key2doublearray_get(var_name, val)
    character*(*), intent(in)  :: var_name  ! name of variable
    real*8,        intent(out) :: val(:)    ! value

    byte_declare :: cname(len(var_name)+1) ! name for C

    call cstring(var_name, cname, '2C')
    call c_key2doublearray_get(cname, val, size(val))
  end subroutine key2doublearray_get


  subroutine key2int_get(var_name, val)
    character*(*), intent(in)  :: var_name  ! name of variable
    integer,       intent(out) :: val       ! value

    byte_declare :: cname(len(var_name)+1) ! name for C

    call cstring(var_name, cname, '2C')
    call c_key2int_get(cname, val)
  end subroutine key2int_get

  subroutine key2intarray_get(var_name, val)
    character*(*), intent(in)  :: var_name  ! name of variable
    integer,       intent(out) :: val(:)    ! value

    byte_declare :: cname(len(var_name)+1) ! name for C

    call cstring(var_name, cname, '2C')
    call c_key2intarray_get(cname, val, size(val))
  end subroutine key2intarray_get

  subroutine key2bool_get(var_name, val)
    character*(*), intent(in)  :: var_name  ! name of variable
    logical,       intent(out) :: val       ! value

    byte_declare :: cname(len(var_name)+1) ! name for C
    integer      :: ival

    ival=0
    call cstring(var_name, cname, '2C')
    call c_key2bool_get(cname, ival)
    val = ival/=0
  end subroutine key2bool_get

  subroutine key2boolarray_get(var_name, val)
    character*(*), intent(in)  :: var_name  ! name of variable
    logical,       intent(out) :: val(:)    ! value

    byte_declare :: cname(len(var_name)+1) ! name for C
    integer      :: ival(size(val))

    ival=0
    call cstring(var_name, cname, '2C')
    call c_key2boolarray_get(cname, ival, size(val))
    val = ival/=0
  end subroutine key2boolarray_get

  subroutine key2write_namelist(filename, namelist_name, mode)
    character(*), intent(in) :: filename
    character(*), intent(in) :: namelist_name
    character(*), intent(in) :: mode              ! 'a' for append

    byte_declare :: cfname(len(filename)+1)       ! C file name
    byte_declare :: cnname(len(namelist_name)+1)  ! C namelist name
    byte_declare :: cmode(len(mode)+1)            ! C mode

    call cstring(filename,      cfname, '2C')
    call cstring(namelist_name, cnname, '2C')
    call cstring(mode,          cmode,  '2C')

    call c_key2write_namelist(cfname, cnname, cmode)
  end subroutine key2write_namelist

end module key_access
