!
!= 2 縺､縺ｮ螟画焚縺ｮ谺｡蜈�ｧ区�縺ｮ蜈ｱ騾壼喧
!
! Authors::   Eizi TOYODA, Yasuhiro MORIKAWA
! Version::   $Id: gtvarlimitbinary.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#Transform
! 縺ｨ縺励※謠蝉ｾ帙＆繧後∪縺吶�

subroutine GTVarXformBinary(var1, var2, err)
  !
  !== 2 縺､縺ｮ螟画焚縺ｮ谺｡蜈��鄂ｮ縺ｮ蜈ｱ騾壼喧
  !
  ! 螟画焚 <b>var1</b> 縺ｮ谺｡蜈�ｧ区�縺� <b>var2</b> 縺ｮ谺｡蜈�ｧ区�縺ｨ蜷後§縺ｫ縺ｪ繧九ｈ縺�↓
  ! 遽�峇諡俶據繧定｡後＞縺ｾ縺吶る℃蜑ｰ縺ｪ谺｡蜈�′ <b>var1</b> 縺ｫ縺ゅｋ蝣ｴ蜷医�國阡ｽ
  ! 繧定｡後＞縺ｾ縺吶�(霑ｽ蜉�繧ゅ〒縺阪ｋ繧医≧縺ｫ縺吶ｋ莠亥ｮ壹〒縺�)縲�
  !
  ! 繧ｨ繝ｩ繝ｼ縺檎函縺倥◆蝣ｴ蜷医√Γ繝�そ繝ｼ繧ｸ繧貞�蜉�
  ! 縺励※繝励Ο繧ｰ繝ｩ繝�縺ｯ蠑ｷ蛻ｶ邨ゆｺ�＠縺ｾ縺吶�*err* 繧剃ｸ弱∴縺ｦ縺ゅｋ蝣ｴ蜷医↓縺ｯ
  ! 縺ｮ蠑墓焚縺ｫ .true. 縺瑚ｿ斐ｊ縲√�繝ｭ繧ｰ繝ｩ繝�縺ｯ邨ゆｺ�＠縺ｾ縺帙ｓ縲�
  !
  !--
  ! 莠後▽縺ｮ螟画焚 var1, var2 縺ｫ蜈･蜃ｺ蜉帷ｯ�峇諡俶據繧貞刈縺医※谺｡蜈��鄂ｮ繧貞�騾壼喧縺吶ｋ縲�
  ! 邨先棡縺ｮ谺｡蜈�ｧ区�縺ｯ縺ｨ繧翫≠縺医★繝｢繝�Ν縺ｧ菴ｿ縺医ｋ繧医≧縺ｫ豎ｺ繧√◆縲�
  !    var2 縺ｮ遨ｺ髢薙ｒ菫晄戟縺吶ｋ縲Ｗar1 繧貞､牙ｽ｢縺吶ｋ縲�
  !    var2 縺ｮ谺｡蜈�� (譛牙ｹ��邵ｮ騾縺ｨ繧ゅ↓) var2 縺ｫ縺翫￠繧句ｹ�′縺ｨ繧峨ｌ繧九�
  !    縺励◆縺後▲縺ｦ var1 縺ｫ縺翫＞縺ｦ縺ｯ蟄伜惠縺励↑縺�° var2 繧偵き繝舌�縺吶ｋ蟷�〒
  !    縺ｪ縺代ｌ縺ｰ縺ｪ繧峨↑縺��
  !    var2 縺ｫ縺ｪ縺� var1 縺ｮ谺｡蜈��隕九∴縺ｪ縺�ｈ縺�↓縺ｪ繧九�縺ｧ邵ｮ騾縺励※縺�ｋ縺�
  !    蟄伜惠縺励↑縺��縺ｧ縺ｪ縺代ｌ縺ｰ縺ｪ繧峨↑縺��
  !++
  use gtdata_types, only: gt_variable
  use gtdata_generic, only: inquire, get_slice
  use gtdata_internal_map, only: map_allocate, map_apply, gt_dimmap, gtvar_dump
  use dc_error, only: StoreError, GT_ENOMATCHDIM, GT_EFAKE, DC_NOERR
  use dc_trace, only: beginsub, endsub, DbgMessage
  implicit none
  type(GT_VARIABLE), intent(inout):: var1, var2
  logical, intent(out), optional:: err
  integer:: ndim1, ndim2, ndimo
  integer, allocatable:: map1(:), map2(:)
  type(GT_DIMMAP), pointer:: newmap(:)
  integer:: i, j, stat
  character(*), parameter:: subnam = "GTVarXformBinary"
continue
  call beginsub(subnam, 'mapid=[%d, %d]', i=(/var1%mapid, var2%mapid/))
  call gtvar_dump(var1)
  call gtvar_dump(var2)
  !
  ! 莠後▽縺ｮ螟画焚 var1, var2 縺九ｉ蜈ｱ譛画ｬ｡蜈�ｒ隱ｿ縺ｹ縲∝ｯｾ蠢懆｡ｨ map1, map2 繧偵▽縺上ｋ縲�
  !
  if (present(err)) err = .false.
  call inquire(var1, alldims=ndim1)
  call inquire(var2, alldims=ndim2)
  ndimo = max(ndim1, ndim2, 0)
  allocate(map1(1:ndim1), map2(1:ndim2))
  call getmatch(var1, var2, ndim1, ndim2, map1, map2)
  call DbgMessage('map1=%*d map2=%*d', i=(/map1(1:ndim1), map2(1:ndim2)/), n=(/ndim1, ndim2/))
  if (all(map2(1:ndim2) == 0)) then
    stat = gt_enomatchdim
    goto 999
  endif
  !
  ! 蜀埼�鄂ｮ繝��繝悶Ν菴懈�髢句ｧ�
  !
  ndimo = ndim2 + count(map1(1:ndim1) == 0)
  call map_allocate(newmap, ndimo)
  !
  ! 1..ndim2 縺ｯ map2 縺ｫ繧医▲縺ｦ var2 縺ｮ谺｡蜈�◆縺｡縺ｫ繝槭ャ繝励☆繧�
  !
  newmap(1:ndim2)%dimno = map2(1:ndim2)
  call inquire(var2, allcount=newmap(1:ndim2)%allcount)
  call get_slice(var2, count=newmap(1:ndim2)%count)
  do, j = 1, ndim2
    if (map2(j) == 0) then
      newmap(j)%start = 1
      newmap(j)%stride = 1
      call inquire(var2, j, url=newmap(j)%url)
    else
      ! 菴咲ｽｮ蟇ｾ蠢懊↓繧医▲縺ｦ var1 蛛ｴ縺ｧ縺ｮ髢句ｧ倶ｽ咲ｽｮ繧呈ｱｺ螳壹☆繧�
      call adjust_slice(var1, var2, map2(j), j, &
        & newmap(j)%start, newmap(j)%stride)
    endif
  enddo
  !
  ! ndim2+1.. ndimo 縺ｯ var2 縺ｫ蟇ｾ蠢懊＆縺帙ｉ繧後↑縺� var1 縺ｮ谺｡蜈�ｒ縺翫￥
  !
  j = 0
  loop1: do, i = ndim2 + 1, ndimo
    do
      j = j + 1
      if (j > ndim1) exit loop1
      if (map1(j) <= 0) exit
    enddo
    newmap(i)%dimno = j
    call inquire(var1, dimord=j, allcount=newmap(i)%allcount)
    call get_slice(var1, dimord=j, start=newmap(i)%start, &
      & count=newmap(i)%count, stride=newmap(i)%stride)
  end do loop1
  !
  call map_apply(var1, map=newmap)
  !
  stat = dc_noerr
999 continue
  call StoreError(stat, subnam, err)
  call endsub(subnam, 'stat=%d', i=(/stat/))
  deallocate(map1, map2)
  return
contains

  !
  ! 莠後▽縺ｮ谺｡蜈�､画焚繧定ｪｿ縺ｹ縲∬ｻｸ荳贋ｽ咲ｽｮ縺悟ｯｾ蠢懊☆繧九ｈ縺�↓
  ! start 繧ｷ繝輔ヨ謨ｰ縺ｨ stride 繝輔ぃ繧ｯ繧ｿ繧呈ｱｺ螳壹☆繧�
  !
  subroutine adjust_slice(var1, var2, idim1, idim2, offset, stepfact)
    use gtdata_generic, only: get, open, close
    type(GT_VARIABLE), intent(in):: var1, var2
    integer, intent(in):: idim1, idim2
    integer, intent(out):: offset, stepfact
    type(GT_VARIABLE):: var_d
    integer:: n, buf(1)
    real, allocatable:: val1(:), val2(:)
  continue
    call beginsub('adjust_slice')
    call open(var_d, source_var=var1, dimord=idim1, count_compact=.true.)
    call inquire(var_d, size=n)
    allocate(val1(n))
    call get(var_d, val1, n)
    call close(var_d)
    !
    call open(var_d, source_var=var2, dimord=idim2, count_compact=.true.)
    call inquire(var_d, size=n)
    allocate(val2(n))
    call get(var_d, val2, n)
    call close(var_d)
    !
    buf(1:1) = minloc(abs(val1(:) - val2(1)))
    offset = buf(1) - 1
    if (size(val2) < 2 .or. size(val1) < 2) then
      stepfact = 1
    else
      buf(1:1) = minloc(abs(val1(:) - val2(2)))
      stepfact = buf(1) - (offset + 1)
    endif
    !
    deallocate(val1, val2)
    call endsub('adjust_slice')
  end subroutine adjust_slice

  !
  ! 莠後▽縺ｮ螟画焚縺九ｉ蜈ｱ譛画ｬ｡蜈�ｒ隱ｿ縺ｹ縲∝ｯｾ蠢懆｡ｨ map1, map2 繧剃ｽ懊ｋ縲�
  ! 縺吶↑繧上■縲√◎繧後◇繧後�谺｡蜈�分蜿ｷ縺九ｉ逶ｸ譁ｹ縺ｮ谺｡蜈�分蜿ｷ繧貞ｾ励ｋ陦ｨ縺ｧ縺ゅｋ縲�
  !
  subroutine getmatch(var1, var2, ndim1, ndim2, map1, map2)
    use dc_types, only: STRING
    use dc_units, only: UNITS, add_okay, assignment(=), clear, deallocate
    use gtdata_generic, only: get_attr, open, close
    type(GT_VARIABLE), intent(in):: var1, var2
    integer, intent(in):: ndim1, ndim2
    integer, intent(out):: map1(:), map2(:)
    type(GT_VARIABLE):: var_d
    integer, allocatable:: map(:, :)
    integer:: i, j
    character(STRING):: su1, su2
    type(UNITS), allocatable:: u1(:), u2(:)
  continue
    call beginsub('getmatch')
    ! 霑泌唆蛟､縺ｯ繝�ヵ繧ｩ繝ｫ繝� 0
    map1(:) = 0
    map2(:) = 0
    ! 陦ｨ縺ｮ讒狗ｯ�: 蛻晄悄蛟､縺ｯ豸亥悉豕輔ｒ縺ｨ繧九％縺ｨ繧堤､ｺ縺�
    allocate(map(ndim1, ndim2))
    map(:, :) = 1

    ! 蜊倅ｽ阪↓繧医ｋ蟇ｾ蠢� --- 蜉�邂怜庄閭ｽ縺ｧ縺ｪ縺代ｌ縺ｰ蟇ｾ縺ｫ縺励↑縺�
    ! 蜊倅ｽ阪�讒区�
    allocate(u1(ndim1), u2(ndim2))
    do, i = 1, ndim1
      call open(var_d, var1, i, count_compact=.true.)
      call get_attr(var_d, 'units', su1)
      call close(var_d)
      call clear(u1(i))
      u1(i) = su1
    enddo
    do, j = 1, ndim2
      call open(var_d, var2, j, count_compact=.true.)
      call get_attr(var_d, 'units', su2)
      call close(var_d)
      call clear(u2(j))
      u2(j) = su2
    enddo
    ! 蜃ｦ逅�
    do, i = 1, ndim1
      do, j = 1, ndim2
        if (.not. add_okay(u1(i), u2(j))) &
          & map(i, j) = 0
      enddo
    enddo
    ! 蜊倅ｽ阪�蟒�｣�
    do, i = 1, ndim1
      call deallocate(u1(i))
    enddo
    do, j = 1, ndim2
      call deallocate(u2(j))
    enddo
    deallocate(u1, u2)

    if (map_finished(map)) goto 1000

    ! --- it fails ---
    call endsub('getmatch', 'fail')
    return

1000 continue
    do, i = 1, ndim1
      call DbgMessage('map(%d, :)=%*d', i=(/i, map(i,:)/), n=(/ndim2/))
    enddo
    do, i = 1, ndim1
      if (all(map(i, :) <= 0)) then
        map1(i) = 0
      else
        map1(i:i) = maxloc(map(i, :))
      endif
    enddo
    do, j = 1, ndim2
      if (all(map(:, j) <= 0)) then
        map2(j) = 0
      else
        map2(j:j) = maxloc(map(:, j), dim=1)
      endif
    enddo
    call endsub('getmatch', 'okay')
  end subroutine getmatch

  logical function map_finished(map) result(result)
    integer:: map(:, :)
    integer:: i, j, ni
  continue
    call beginsub('map_finished')
    ni = size(map, dim=1)
    do, i = 1, ni
      if (count(map(i, :) > 0) > 1) then
        result = .false.
        goto 999
      endif
    enddo
    do, j = 1, ni
      if (count(map(j, :) > 0) > 1) then
        result = .false.
        goto 999
      endif
    enddo
    result = .true.
999 continue
    call endsub('map_finished')
  end function map_finished

end subroutine GTVarXformBinary