!
!= ç‹¬ç«‹å¤‰æ•° (æ¬¡å…ƒ) ã®ä½œæˆ
!
! Authors::   Yasuhiro MORIKAWA, Eizi TOYODA
! Version::   $Id: gtvarcreated.f90,v 1.5 2009-05-25 09:55:58 morikawa Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2000-2005. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
! ä»¥ä¸‹ã®ã‚µãƒ–ãƒ«ãƒ¼ãƒãƒ³, é–¢æ•°ã¯ gtdata_generic ã‹ã‚‰æä¾›ã•ã‚Œã¾ã™ã€‚
!

subroutine GTVarCreateD(var, url, length, xtype, long_name, overwrite, err)
  !
  !== ç‹¬ç«‹å¤‰æ•° (æ¬¡å…ƒ) ã®ä½œæˆ
  !
  ! å ´æ‰€ *url* ã«é•·ã• *length* ã®è‡ªåˆ†è‡ªèº«ã‚’æ¬¡å…ƒã¨ã™ã‚‹å¤‰æ•°ã¤ã¾ã‚Š GT_VARIABLE åž‹
  ! ã®å®Ÿä½“ã‚’ä½œæˆã—ã€ãã‚Œã‚’ç¬¬ 1 å¼•æ•° *var* ã«ã‚»ãƒƒãƒˆã—ã¾ã™ã€‚
  ! Open ã•ã‚ŒãŸã‚‚ã®ã¨åŒæ§˜ã€ç¬¬1å¼•æ•° *var* ã¯å¾Œã§å¿…ãš
  ! Close ã•ã‚Œãªã‘ã‚Œã°ãªã‚Šã¾ã›ã‚“ã€‚
  !
  ! é•·ã• length == 0 ã‚’æŒ‡å®šã™ã‚‹ã¨ãã®å¤‰æ•°ã¯å¯å¤‰é•·æ¬¡å…ƒã¨ãªã‚Šã¾ã™ã€‚
  ! åž‹ *xtype* ã‚’çœç•¥ã™ã‚‹ã¨ "+float+" ã¨
  ! ã¿ãªã•ã‚Œã¾ã™ã€‚æ—¢å˜å¤‰æ•°ãŒã‚ã‚‹ã¨ãå¤±æ•—ã—ã¾ã™ãŒã€
  ! overwrite == .true. ã§ã‚ã‚Œã°ä¸Šæ›¸ãã—ã¦ç¶šè¡Œã—ã¾ã™ã€‚
  ! (ã¾ã  *overwrite* ã®å‹•ä½œã¯ä¿éšœã•ã‚Œã¦ã„ã¾ã›ã‚“)ã€‚
  ! dims ã®çœç•¥ã¯ 0 æ¬¡å…ƒå¤‰æ•°ã®è¨å®šã‚’æ„å‘³ã—ã¾ã™ã€‚
  !
  ! æ¬¡å…ƒå¤‰æ•°ã¯è‡ªå‹•ç”Ÿæˆã•ã‚Œã‚‹ã“ã¨ãŒå¤šã„ãŸã‚ã€å¤‰æ•°åéƒ¨ã‚’æ¬ ãæŒ‡å®šã«å¯¾ã—ã¦ã¯
  ! åå‰ã‚’è‡ªå‹•ç”Ÿæˆã—ã¾ã™ã€‚
  !
  ! ä½œæˆã®éš›ã«ã‚¨ãƒ©ãƒ¼ãŒç”Ÿã˜ãŸå ´åˆã€ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã‚’å‡ºåŠ›ã—ã¦ãƒ—ãƒã‚°ãƒ©ãƒ ã¯
  ! å¼·åˆ¶çµ‚äº†ã—ã¾ã™ã€‚*err* ã‚’ä¸Žãˆã¦ã‚ã‚‹å ´åˆã«ã¯ã“ã®å¼•æ•°ã« .true.
  ! ãŒè¿”ã‚Šã€ãƒ—ãƒã‚°ãƒ©ãƒ ã¯çµ‚äº†ã—ã¾ã›ã‚“ã€‚
  !
  use dc_string, only: StrHead
  use gtdata_types, only: GT_VARIABLE
  use gtdata_generic, only: GtDataTmpNam
  use gtdata_netcdf_generic, only: Create, Put_Attr
  use gtdata_netcdf_types, only: GD_NC_VARIABLE
  use gtdata_memory_generic, only: Create
  use gtdata_memory_types, only: GD_MEM_VARIABLE
  use gtdata_internal_map, only: map_create, vtb_class_memory, vtb_class_netcdf, gtvar_dump
  use dc_url, only: UrlSplit, UrlMerge
  use dc_trace, only: BeginSub, EndSub, DbgMessage
  use dc_error, only: StoreError, DC_NOERR
  use dc_types, only: STRING
  implicit none
  type(GT_VARIABLE), intent(out):: var
  character(len = *), intent(in):: url
  integer, intent(in):: length
  character(len = *), intent(in), optional:: xtype
  character(len = *), intent(in), optional:: long_name
  logical, intent(in), optional:: overwrite
  logical, intent(out), optional:: err
  character(len = STRING):: fnam, vnam, new_url, data_class
  type(GD_NC_VARIABLE):: gdnc
  type(GD_MEM_VARIABLE):: mem
  integer :: stat, cause_i
  character(len = *),      parameter:: subname = "GTVarCreateD"
  character(len = *),      parameter:: version = &
    & '$Name:  $' // &
    & '$Id: gtvarcreated.f90,v 1.5 2009-05-25 09:55:58 morikawa Exp $'
continue
  call BeginSub(subname, 'url=<%c> length=%d', &
    & c1=trim(url), i=(/length/), version=version)
  stat = DC_NOERR
  cause_i = 0
  data_class = ''
  if (strHead(url, "memory:")) then
    call Create(mem, url, length, xtype, long_name, overwrite, err)
    call map_create(var, vtb_class_memory, mem%id, 1, (/length/), stat)
    if (stat /= DC_NOERR) then
      cause_i = 1
      goto 999
    end if
    call gtvar_dump(var)
    data_class = 'memory'
    goto 999
  endif
  ! URL ã®æ¤œæŸ»
  call UrlSplit(url, file=fnam, var=vnam)
  if (vnam == "") then
    call GtDataTmpNam(file=fnam, base="dim", result=new_url)
  else
    new_url = url
  endif
  ! gdnc å½¢å¼ãŒé¸æŠžã•ã‚Œã‚‹å ´åˆã¯
  call Create(var=gdnc, url=new_url, length=length, xtype=xtype, &
    & overwrite=overwrite, err=err)
  if (present(long_name)) then
    call put_attr(gdnc, 'long_name', long_name, err=err)
  endif
  call map_create(var, vtb_class_netcdf, gdnc%id, 1, (/length/), stat)
    if (stat /= DC_NOERR) then
      cause_i = 1
      goto 999
    end if
  call gtvar_dump(var)
  data_class = 'netcdf'
999 continue
  call StoreError(stat, subname, err, cause_i=cause_i)
  call EndSub(subname, 'class=%c mapid=%d', &
    & c1=trim(data_class), i=(/var%mapid/) )
end subroutine GTVarCreateD