!= gtvaropen.f90 - gtool4 ãƒ‡ãƒ¼ã‚¿ã®ã‚ªãƒ¼ãƒ—ãƒ³
!
! Authors::   Yasuhiro MORIKAWA, Eizi TOYODA
! Version::   $Id: gtvaropen.f90,v 1.4 2009-05-25 09:55:57 morikawa Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2000-2005. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
! ä»¥ä¸‹ã®ã‚µãƒ–ãƒ«ãƒ¼ãƒãƒ³, é–¢æ•°ã¯ gtdata_generic ã‹ã‚‰æä¾›ã•ã‚Œã¾ã™ã€‚
!

subroutine GTVarOpen(var, url, writable, err)
  !
  !== gtool4 ãƒ‡ãƒ¼ã‚¿ã®ã‚ªãƒ¼ãƒ—ãƒ³
  !
  ! *url* ã§è˜åˆ¥ã•ã‚Œã‚‹ gtool å¤‰æ•°ã‚’é–‹ãã€*var* ã«æ ¼ç´ã—ã¾ã™ã€‚
  ! *writable* ã‚’ <tt>.true.</tt> ã«æŒ‡å®šã™ã‚‹ã¨æ›¸ãè¾¼ã¿å¯ã§é–‹ã“ã†ã¨ã—ã¾ã™ã€‚
  ! ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆã¯æ›¸ãè¾¼ã¿ä¸å¯ã§é–‹ãã¾ã™ã€‚
  ! (ã¾ã  *writable* ã®å‹•ä½œã¯ä¿éšœã•ã‚Œã¦ã„ã¾ã›ã‚“)ã€‚
  !
  ! Open ã•ã‚ŒãŸå¤‰æ•°ã¯å¿…ãš Close ã•ã‚Œãªã‘ã‚Œã°ãªã‚Šã¾ã›ã‚“ã€‚
  !
  ! ã‚¨ãƒ©ãƒ¼ãŒç™ºç”Ÿã—ãŸå ´åˆã€å¼•æ•° *err* ãŒä¸Žãˆã‚‰ã‚Œã‚‹å ´åˆã¯ *err* ãŒ
  ! <tt>.true.</tt> ã¨ãªã£ã¦è¿”ã‚Šã¾ã™ã€‚
  ! å¼•æ•° *err* ã‚’ä¸Žãˆãªã‘ã‚Œã°ãƒ—ãƒã‚°ãƒ©ãƒ ã¯åœæ¢ã—ã¾ã™ã€‚
  !
  ! *Open* ã¯ 2 ã¤ã®ã‚µãƒ–ãƒ«ãƒ¼ãƒãƒ³ã®ç·ç§°åã§ã‚ã‚Šã€
  ! ã‚ã‚‹å¤‰æ•°ã®æ¬¡å…ƒã‚’æŒ‡å®šã™ã‚‹ã“ã¨ã§é–‹ãã“ã¨ã‚‚å¯èƒ½ã§ã™ã€‚
  ! ä¸Šè¨˜ã®ã‚µãƒ–ãƒ«ãƒ¼ãƒãƒ³ã‚’å‚ç…§ãã ã•ã„ã€‚
  !
  !=== è£œè¶³
  !
  ! ä¸Šè¨˜ã® Open ã‚’å‚ç…§ã—ã¦ãã ã•ã„ã€‚
  !
  use dc_string, only: StrHead
  use dc_types, only: STRING
  use gtdata_types, only: GT_VARIABLE
  use gtdata_generic, only: limit
  use gtdata_internal_map, only: map_create, vtb_class_netcdf, vtb_class_memory, gtvar_dump
  use gtdata_netcdf_generic, only: Open, Inquire
  use gtdata_netcdf_types, only: GD_NC_VARIABLE
  use dc_url, only: url_chop_iorange
  use dc_error, only: StoreError, GT_EFAKE, DC_NOERR, GT_ENOTVAR
  use dc_trace, only: beginsub, endsub
  use dc_present, only: present_and_true
  implicit none
  type(GT_VARIABLE), intent(out):: var
  character(*), intent(in):: url
  logical, intent(in), optional:: writable
  logical, intent(out), optional:: err
  integer:: ndims, stat, cause_i
  character(STRING):: cause_c
  integer, allocatable:: dimlen(:)
  type(GD_NC_VARIABLE):: gdnc
  character(STRING):: filevar, iorange
  character(*),      parameter:: subname = "GTVarOpen"
  character(*),      parameter:: version = &
    & '$Name:  $' // &
    & '$Id: gtvaropen.f90,v 1.4 2009-05-25 09:55:57 morikawa Exp $'
continue
  call beginsub(subname, fmt='<%c>', c1=trim(url), version=version)
  stat = DC_NOERR
  cause_i = 0
  cause_c = ''
  var = GT_VARIABLE(-1)
  call url_chop_iorange(url, iorange=iorange, remainder=filevar)
  if (StrHead(filevar, "memory:")) then
    stat = GT_EFAKE
    cause_c = 'GTVarOpen(memory:)'
    goto 999
  else
    call Open(gdnc, filevar, writable, err)
    if ( present_and_true(err) ) then
      stat = GT_ENOTVAR
      goto 999
    end if
    call inquire(gdnc, ndims=ndims)
    allocate(dimlen(max(1, ndims)))
    call inquire(gdnc, dimlen=dimlen)
    call map_create(var, vtb_class_netcdf, gdnc%id, ndims, dimlen, stat)
    if (stat /= DC_NOERR) then
      cause_i = ndims
      goto 999
    end if
    deallocate(dimlen)
  endif
  call limit(var, trim(iorange))
  call gtvar_dump(var)
999 continue
  call StoreError(stat, subname, err, cause_c = cause_c, cause_i = cause_i)
  call endsub(subname, 'mapid=%d', i=(/var%mapid/))
end subroutine GTVarOpen