!
!= æ¬¡å…ƒã®è¿½åŠ
!
! Authors::   Eizi TOYODA, Yasuhiro MORIKAWA
! Version::   $Id: gtvaradddim.f90,v 1.3 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 ã‹ã‚‰ gtdata_generic#Add_dim
! ã¨ã—ã¦æä¾›ã•ã‚Œã¾ã™ã€‚

subroutine GTVarAddDim(var, dimord, dimvar, err)
  !
  !== æ¬¡å…ƒã®è¿½åŠ
  !
  ! å¤‰æ•° *var* ã® *dimord* ç•ªç›®ã®ä½ç½®ã«æ¬¡å…ƒ *dimvar*
  ! ã‚’è¿½åŠ ã—ã¾ã™ã€‚*dimord* ç•ªç›®ä»¥é™ã®æ¬¡å…ƒã¯ 1 ã¤å¾Œã‚ã«ãšã‚Œã¾ã™ã€‚
  ! ã‚‚ã— *dimord* ãŒ *var* ã®æœ‰åŠ¹æ¬¡å…ƒæ•°ã‚ˆã‚Šã‚‚å¤§ãã„å ´åˆã€
  ! (æœ‰åŠ¹æ¬¡å…ƒæ•° + 1) ãŒä¸Žãˆã‚‰ã‚ŒãŸã‚‚ã®ã¨è¦‹ãªã•ã‚Œã¾ã™ã€‚
  !
  ! ã‚¨ãƒ©ãƒ¼ãŒç”Ÿã˜ãŸå ´åˆã€ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã‚’å‡ºåŠ›
  ! ã—ã¦ãƒ—ãƒã‚°ãƒ©ãƒ ã¯å¼·åˆ¶çµ‚äº†ã—ã¾ã™ã€‚*err* ã‚’ä¸Žãˆã¦ã‚ã‚‹å ´åˆã«ã¯
  ! ã®å¼•æ•°ã« .true. ãŒè¿”ã‚Šã€ãƒ—ãƒã‚°ãƒ©ãƒ ã¯çµ‚äº†ã—ã¾ã›ã‚“ã€‚
  !
  use gtdata_types, only: GT_VARIABLE
  use gtdata_generic, only: inquire
  use gtdata_internal_map, only: map_lookup, gt_dimmap, map_set_ndims, map_set, map_resize
  use dc_trace, only: beginsub, endsub, DbgMessage
  implicit none
  type(GT_VARIABLE), intent(in):: var
  type(GT_VARIABLE), intent(in):: dimvar
  integer, intent(in):: dimord
  logical, intent(out):: err
  type(gt_dimmap), pointer:: map(:)
  type(gt_dimmap):: tmpmap
  integer:: id, nd, ndimsp, stat, vid
  character(*), parameter:: subname = 'GTVarAddDim'
continue
  err = .true.
  call beginsub(subname)

  if (dimord < 1) then
    call endsub(subname, "negative dimord=%d invalid", i=(/dimord/))
    return
  endif

  ! dimvar ã‚’ãƒã‚§ãƒƒã‚¯ã—ãƒžãƒƒãƒ—è¨å®šã‚’ tmpmap ã«ä¿å˜
  call map_lookup(dimvar, vid=vid, ndims=nd)
  if (vid < 0) then
    call endsub(subname, "dimvar invalid")
    return
  endif
  if (nd <= 0) then
    call endsub(subname, "dimvar nondimensional")
    return
  else if (nd > 1) then
    call endsub(subname, "dimvar multidimensional")
    return
  endif
  allocate(map(nd))
  call map_lookup(dimvar, map=map)
  tmpmap = map(1)
  deallocate(map)

  ! dimord ç•ªç›® (ãŸã ã— ndimsp + 1 ã‚’è¶Šãˆãªã„) ã«æŒ¿å…¥ã™ã‚‹éš™é–“ã‚’ã‚ã‘ã‚‹
  call map_lookup(var, ndims=ndimsp)
  if (dimord > ndimsp + 1) then
    id = ndimsp + 1
  else
    id = dimord
  endif
  allocate(map(nd + 1))
  call map_resize(var, nd + 1)
  call map_lookup(var, map=map)
  map(id+1: nd+1) = map(id: nd)

  ! æ–°ã—ã„æ¬¡å…ƒã¸ã®å‚ç…§ã‚’æŒ¿å…¥
  map(id)%dimno = -1
  call inquire(dimvar, url=map(id)%url)
  map(id)%allcount = tmpmap%allcount
  map(id)%offset = tmpmap%offset
  map(id)%step = tmpmap%step
  map(id)%start = tmpmap%start
  map(id)%count = tmpmap%count
  map(id)%stride = tmpmap%stride

  ! ç™»éŒ²
  call map_set(var, map=map, stat=stat)
  if (stat /= 0) goto 999
  call map_set_ndims(var, ndims=ndimsp + 1, stat=stat)

999 continue
  err = (stat /= 0)
  call endsub(subname)
end subroutine GTVarAddDim