!
!= å…¥å‡ºåŠ›ç¯„å›²ã‚’ç§»å‹•
!
! Authors::   Eizi TOYODA, Yasuhiro MORIKAWA
! Version::   $Id: gtvarslicenext.f90,v 1.3 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 ã‹ã‚‰ gtdata_generic#Slice_Next
! ã¨ã—ã¦æä¾›ã•ã‚Œã¾ã™ã€‚

subroutine GTVarSliceNext(var, dimord, err, stat)
  !
  !== å…¥å‡ºåŠ›ç¯„å›²ã‚’ç§»å‹•
  !
  ! å¤‰æ•° *var* ã® *dimord* ç•ªç›®ã®æ¬¡å…ƒã® *start* å€¤ã‚’ *stride* *
  ! *count* å€‹ã ã‘å¢—ã‚„ã™ã“ã¨ã«ã‚ˆã£ã¦æ¬¡å…ƒç¯„å›²ã‚’ç§»å‹•ã—ã¾ã™ã€‚*dimord*
  ! ã‚’çœç•¥ã™ã‚‹ã¨ã€ã©ã‚Œã‹ã®æ¬¡å…ƒã«ã¤ã„ã¦ã“ã®æ“ä½œã‚’è¡Œã„ã¾ã™ã€‚æˆåŠŸã—ãŸ
  ! å ´åˆ *stat* ãŒ 0 ã«ãªãƒªã¾ã™ã€‚
  !
  ! ã„ãšã‚Œã‹ã®æ¬¡å…ƒã«ã¤ã„ã¦ *start*, *stride* å€¤ãŒ 1 ã«ãªã‚‹ã‚ˆã†ãª
  ! Slice ã‚’è¨å®šã—ã¦ãŠã„ã¦ã€Slice_Next ã‚’é †æ¬¡å‘¼ã³å‡ºã™ã¨å¤‰æ•°å…¨ä½“
  ! ã‚’èµ°æŸ»ã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚
  !
  ! å…¥å‡ºåŠ›ç¯„å›²ã‚’ç§»å‹•ã™ã‚‹éš›ã«ã‚¨ãƒ©ãƒ¼ãŒç”Ÿã˜ãŸå ´åˆã€ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã‚’å‡ºåŠ›
  ! ã—ã¦ãƒ—ãƒã‚°ãƒ©ãƒ ã¯ å¼·åˆ¶çµ‚äº†ã—ã¾ã™ã€‚*err* ã‚’ä¸Žãˆã¦ã‚ã‚‹å ´åˆã«ã¯
  ! ã®å¼•æ•°ã« .true. ãŒè¿”ã‚Šã€ãƒ—ãƒã‚°ãƒ©ãƒ ã¯çµ‚äº†ã—ã¾ã›ã‚“ã€‚
  !
  use gtdata_types, only: GT_VARIABLE
  use dc_error, only: GT_EFAKE, StoreError, DC_NOERR, gt_enomoredims, &
    & NF90_EINVAL, NF90_ENOTVAR
  use gtdata_internal_map, only: map_lookup, gt_dimmap, map_set
  use dc_trace, only: beginsub, endsub, DbgMessage
  implicit none
  type(GT_VARIABLE), intent(in out):: var
  integer, intent(in), optional:: dimord
  logical, intent(out), optional:: err
  integer, intent(out), optional:: stat
  type(gt_dimmap), allocatable:: map(:)
  integer:: mystat, vid, id, nd, idim_lo, idim_hi, ilast
continue
  call beginsub('gtvarslicenext')
  if (present(dimord)) call DbgMessage('dimord=%d', i=(/dimord/))

  call map_lookup(var, vid=vid, ndims=nd)
  if (vid < 0) then
    mystat = NF90_ENOTVAR
    goto 999
  endif
  if (nd <= 0) then
    call DbgMessage('dimension map not associated')
    mystat = gt_enomoredims
    goto 999
  endif
  allocate(map(nd))
  call map_lookup(var, map=map)

  if (present(dimord)) then
    if (dimord < 0 .or. dimord <= size(map)) then
      call DbgMessage('dimord=%d is out of 1..%d', i=(/dimord, size(map)/))
      mystat = NF90_EINVAL
      goto 995
    endif
    idim_lo = dimord
    idim_hi = dimord
  else
    idim_lo = 1
    idim_hi = size(map)
  endif
  call DbgMessage('idim scan range=(%d:%d)', i=(/idim_lo, idim_hi/))

  mystat = gt_enomoredims
  do, id = idim_lo, idim_hi
    ilast = map(id)%start + (map(id)%count * 2 - 1) * map(id)%stride
    call DbgMessage('last_index=%d allcount=%d', &
      & i=(/ilast, map(id)%allcount/))
    if (ilast >= 1 .and. ilast <= map(id)%allcount) then
      map(id)%start = map(id)%start + map(id)%count * map(id)%stride
      mystat = dc_noerr
      exit
    endif
  enddo
  if (mystat /= dc_noerr) goto 995
  call map_set(var, map, mystat)

995 continue
  deallocate(map)

999 continue
  if (present(stat)) then
    stat = mystat
    if (present(err)) err = (mystat /= DC_NOERR)
  else
    call StoreError(mystat, "GTVarSliceNext", err)
  endif
  call endsub('gtvarslicenext', 'stat=%d', i=(/mystat/))
end subroutine GTVarSliceNext