! Copyright (C) GFD Dennou Club, 2000.  All rights reserved.

! dimord 番目の次元について入出力範囲指定を取り出す。
!
subroutine GTVarGetSlice(var, dimord, start, count, stride, count_compact)
    use gtdata_types, only: GT_VARIABLE
    use gt_map, only: gt_dimmap, map_lookup, dimord_skip_compact
    implicit none
    type(gt_variable), intent(in):: var
    integer, intent(in):: dimord
    integer, intent(out), optional:: start
    integer, intent(out), optional:: count
    integer, intent(out), optional:: stride
    logical, intent(in), optional:: count_compact
    type(gt_dimmap), pointer:: map(:)
    integer:: vid, udimord, ndims
    logical:: allmode
continue
    allmode = .true.
    if (present(count_compact)) allmode = count_compact
    call map_lookup(var, vid=vid, ndims=ndims)
    if (vid < 0 .or. ndims <= 0) goto 999
    allocate(map(ndims))
    call map_lookup(var, map=map)
    if (allmode) then
        udimord = dimord
    else
        udimord = dimord_skip_compact(dimord, map)
    endif
    if (udimord < 1 .or. udimord > size(map)) goto 997

    if (present(start)) start = map(udimord)%start
    if (present(count)) count = map(udimord)%count
    if (present(stride)) stride = map(udimord)%stride
    deallocate(map)
    return

997 continue
    deallocate(map)
999 continue
    if (present(start)) start = -1
    if (present(count)) count = -1
    if (present(stride)) stride = -1
end subroutine
