!
!= netCDF 繝輔ぃ繧､繝ｫ縺ｸ谺｡蜈�､画焚菴懈�
!
! Authors::   Eizi TOYODA, Yasuhiro MORIKAWA
! Version::   $Id: gdncvarcreated.f90,v 1.3 2009-10-11 07:36:34 morikawa Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2000-2005. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
! 莉･荳九�繧ｵ繝悶Ν繝ｼ繝√Φ縲�未謨ｰ縺ｯ gtdata_netcdf_generic 縺九ｉ gtdata_netcdf_generic#Create
! 縺ｨ縺励※謠蝉ｾ帙＆繧後∪縺吶�

subroutine GDNcVarCreateD(var, url, xtype, length, overwrite, err)
  !
  !== 谺｡蜈�､画焚菴懈�
  !
  ! 螟画焚 URL *url* 縺ｫ谺｡蜈�､画焚繧剃ｽ懈�縺励∪縺�.
  ! 谺｡蜈�､画焚縺ｮ髟ｷ縺輔ｒ *length* 縺ｫ荳弱∴縺ｾ縺�.
  ! 霑斐＆繧後ｋ蠑墓焚 *var* 縺ｫ縺ｯ螟画焚 ID 縺ｪ縺ｩ縺ｮ諠��ｱ縺梧�ｼ邏阪＆繧後∪縺�.
  !
  ! *overwrite* 縺ｫ .true. 繧定ｨｭ螳壹☆繧九→荳頑嶌縺榊庄閭ｽ縺ｪ繝｢繝ｼ繝峨↓縺ｪ繧翫∪縺�.
  ! 繝�ヵ繧ｩ繝ｫ繝医�荳頑嶌縺堺ｸ榊庄縺ｧ縺�.
  ! *err* 繧剃ｸ弱∴繧句�ｴ蜷�, 谺｡蜈�､画焚逕滓�譎ゅ↓繧ｨ繝ｩ繝ｼ縺檎函縺倥※繧�
  ! 繝励Ο繧ｰ繝ｩ繝�繧堤ｵゆｺ�○縺�, *err* 縺ｫ .false. 縺瑚ｿ斐ｊ縺ｾ縺�.
  !
  use gtdata_netcdf_types, only: GD_NC_VARIABLE, GD_NC_VARIABLE_SEARCH
  use gtdata_netcdf_internal, only: vtable_add
  use dc_string, only: strieq
  use dc_types, only: string
  use dc_url, only: UrlSplit
  use dc_trace, only: BeginSub, EndSub, DbgMessage
  use netcdf, only: NF90_NOERR, NF90_FLOAT, NF90_INT, NF90_DOUBLE, NF90_CHAR, &
    & NF90_DEF_VAR, NF90_DEF_DIM
  use gtdata_netcdf_file_generic, only: GDNcFileOpen, GDNcFileDefineMode
  use dc_error, only: StoreError, gt_enomem
  implicit none
  type(GD_NC_VARIABLE), intent(out):: var
  character(len = *), intent(in):: url
  character(len = *), intent(in):: xtype
  integer, intent(in):: length
  logical, intent(in), optional:: overwrite
  logical, intent(out), optional:: err
  type(GD_NC_VARIABLE_SEARCH):: ent
  character(len = string):: filename, varname, cause_c
  integer:: stat
  integer:: nc_xtype
  character(len = *), parameter:: subname = "GDNcVarCreateD"
continue
  call BeginSub(subname, 'url=<%c>, xtype=<%c>, length=<%d>', &
    & c1=trim(url), c2=trim(xtype), i=(/length/))
  cause_c = trim(url)
  !
  ! --- 繝輔ぃ繧､繝ｫ繧堤畑諢� ---
  call UrlSplit(url, file=filename, var=varname)
  call GDNcFileOpen(ent%fileid, filename, stat=stat, writable=.TRUE., &
    & overwrite=overwrite)
  if (stat /= NF90_NOERR) goto 999
  stat = GDNcFileDefineMode(ent%fileid)
  if (stat /= NF90_NOERR) goto 999
  !
  ! --- 蝙九�豎ｺ螳� ---
  nc_xtype = NF90_FLOAT
  if (strieq(xtype, "double") .or. strieq(xtype, "DOUBLEPRECISION")) then
    nc_xtype = NF90_DOUBLE
  endif
  if (strieq(xtype, "int") .or. strieq(xtype, "INTEGER")) then
    nc_xtype = NF90_INT
  endif
  if (strieq(xtype, "char") .or. strieq(xtype, "CHARACTER")) then
    nc_xtype = NF90_CHAR
  endif
  !
  ! --- 谺｡蜈�､画焚縺ｮ菴懈� ---
  stat = NF90_DEF_DIM(ent%fileid, trim(varname), len=length, dimid=ent%dimid)
  if (stat /= NF90_NOERR) goto 999
  stat = NF90_DEF_VAR(ent%fileid, trim(varname), &
    & xtype=nc_xtype, dimids=(/ent%dimid/), varid=ent%varid)
  if (stat /= NF90_NOERR) goto 999
  !
  stat = vtable_add(var, ent)
  if (stat /= NF90_NOERR) goto 999

999 continue
  call StoreError(stat, subname, err, cause_c=cause_c)
  if (stat /= NF90_NOERR) var = GD_NC_VARIABLE(-1)
  call EndSub(subname, 'stat=%d', i=(/stat/))
end subroutine GDNcVarCreateD