!c Description:
!c  物性値をデータファイル(NetCDF 形式)から取得する
!c
!c Current Code Owner: 
!c   sugiyama@gfd-dennou.org
!c
!c Histry: 
!c   Version    Date          Comment
!c   -------    ----------    --------
!c   1.0        2003-02-05    作成
!c
!c Copyright (C) SUGIYAMA Ko-ichiro, 2003, All rights reserved

module interface_def_property
  interface

     subroutine def_property(sname, propertydir, property)
       use prm_type, only: type_property
       character(*), intent(in)                      :: sname(:)
       character(*), intent(in)                      :: propertydir
       type(type_property), intent(out), allocatable :: property(:)
     end subroutine def_property

  end interface
end module interface_def_property



subroutine def_property(sname, propertydir, property)
  use interface_netcdf_open
  use interface_netcdf_close
  use interface_netcdf_get
  use interface_list_insert
  use prm_type, only: type_property

  implicit none
  character(*), intent(in)                      :: sname(:)
  character(*), intent(in)                      :: propertydir
  type(type_property), intent(out), allocatable :: property(:)
    
  real(8), allocatable          :: rvar1(:)
  real(8), allocatable          :: rvar2(:)
  real(8), allocatable          :: rvar3(:)
  real(8), allocatable          :: rvar4(:)
  real(8), allocatable          :: rvar5(:)
  integer                       :: ncid
  integer                       :: s
  integer                       :: N  
  integer                       :: i
  integer                       :: ivar
  integer, allocatable          :: ivar1(:)
  character(80)                 :: file
  character(80)                 :: name


  N = size(sname, 1)
  allocate(property(N))
  
  do s = 1, N
     !ポインタ準備
     allocate(property(s)%comps, property(s)%cp, property(s)%svap)
     allocate(property(s)%comps%next, &
          & property(s)%cp%next, property(s)%svap%next)

     !番兵作成
     property(s)%comps%record%atmnum = huge(property(s)%comps%record%atmnum)
     property(s)%comps%record%enum = huge(property(s)%comps%record%enum)
     property(s)%cp%record%temp = huge(property(s)%cp%record%temp)
     property(s)%cp%record%data = huge(property(s)%cp%record%data)
     property(s)%svap%record%temp = huge(property(s)%svap%record%temp)
     property(s)%svap%record%data = huge(property(s)%svap%record%data)
     
     !初期化
     nullify(property(s)%comps%next, &
          & property(s)%cp%next, property(s)%svap%next)
     
     !ファイル名準備
     file = trim(PropertyDir) // trim(sname(s)) // '.nc'
     
     !ファイルオープン
     call netcdf_open(trim(file), ncid)
     
     !化学種の名称
     property(s)%name = trim(sname(s))

     !分子量
     name = "molwt"
     call netcdf_get(ncid, name, property(s)%molwt)
       
     !標準状態でのエントロピー
     name = "entr_ref"
     call netcdf_get(ncid, name, property(s)%ref%entr)
       
     !標準状態でのエンタルピー
     name = "enth_ref"
     call netcdf_get(ncid, name, property(s)%ref%enth)
       
     !水溶液のスイッチ
     name = "solsw"
     call netcdf_get(ncid, name, ivar)
     if (ivar == 0) then 
        property(s)%solsw = .false. 
     elseif(ivar == 1) then 
        property(s)%solsw = .true. 
     end if
     
     !計算方法
     name = "method"
     call netcdf_get(ncid, name, ivar)
     property(s)%method = ivar
     
     !原子番号/ 原子数
     name = "atmnum"
     call netcdf_get(ncid, name, ivar1)
     name = "enum"
     call netcdf_get(ncid, name, rvar3)

     do i = 1, size(ivar1, 1)
        call list_insert(ivar1(i), rvar3(i), property(s)%comps)
     end do

     if (property(s)%method == 1 .OR. property(s)%method == 2) then 
        !比熱
        name = "temp"
        call netcdf_get(ncid, name, rvar1)
        name = "cp"
        call netcdf_get(ncid, name, rvar2)
        do i = 1, size(rvar1, 1)
           call list_insert(rvar1(i), rvar2(i), property(s)%cp)
        end do
     elseif (property(s)%method == 3 .OR. property(s)%method == 4) then 
        !蒸気圧
        name = "temp"
        call netcdf_get(ncid, name, rvar4)
        name = "svap"
        call netcdf_get(ncid, name, rvar5)

        do i = 1, size(rvar4, 1)
           call list_insert(rvar4(i), rvar5(i), property(s)%svap)
        end do
     end if
     
     !ファイルクローズ
     call netcdf_close(ncid)
     
     if (allocated(rvar1)) deallocate(rvar1, rvar2)
     if (allocated(rvar4)) deallocate(rvar4, rvar5)
     if (allocated(ivar1)) deallocate(ivar1, rvar3)
  end do



  
end subroutine def_property
  



