!
!= å¤‰æ•°ã®å°å—
!
! Authors::   Yasuhiro MORIKAWA, Eizi TOYODA
! Version::   $Id: gtvarputline.f90,v 1.1 2009-03-20 09:09:51 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#PutLine
! ã¨ã—ã¦æä¾›ã•ã‚Œã¾ã™ã€‚

subroutine GTVarPutLine( var, unit, indent, err )
  !
  ! å¼•æ•° *var* ã«è¨å®šã•ã‚Œã¦ã„ã‚‹æƒ…å ±ã‚’å°å—ã—ã¾ã™.
  ! ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆã§ã¯ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã¯æ¨™æº–å‡ºåŠ›ã«å‡ºåŠ›ã•ã‚Œã¾ã™.
  ! *unit* ã«è£…ç½®ç•ªå·ã‚’æŒ‡å®šã™ã‚‹ã“ã¨ã§, å‡ºåŠ›å…ˆã‚’å¤‰æ›´ã™ã‚‹ã“ã¨ãŒå¯èƒ½ã§ã™.
  !
  ! Print information of *var*.
  ! By default messages are output to standard output.
  ! Unit number for output can be changed by *unit* argument.
  !
  use dc_types, only: STRING, STDOUT
  use gtdata_types, only: GT_VARIABLE
  use dc_error, only: ErrorCode, StoreError, DC_NOERR, GT_ENOMEM
  use dc_string, only: toChar, Printf, PutLine
  use gtdata_generic, only: Get, Inquire
  use dc_trace, only: BeginSub, EndSub, DbgMessage
  implicit none
  type(GT_VARIABLE), intent(in):: var
  integer, intent(in), optional:: unit
                              ! å‡ºåŠ›å…ˆã®è£…ç½®ç•ªå·.
                              ! ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆã®å‡ºåŠ›å…ˆã¯æ¨™æº–å‡ºåŠ›.
                              !
                              ! Unit number for output.
                              ! Default value is standard output.
  character(*), intent(in), optional:: indent
                              ! è¡¨ç¤ºã•ã‚Œã‚‹ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸ã®å—ä¸‹ã’.
                              !
                              ! Indent of displayed messages.
  logical, intent(out), optional:: err
                              ! ä¾‹å¤–å‡¦ç†ç”¨ãƒ•ãƒ©ã‚°.
                              ! ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆã§ã¯, ã“ã®æ‰‹ç¶šãå†…ã§ã‚¨ãƒ©ãƒ¼ãŒ
                              ! ç”Ÿã˜ãŸå ´åˆ, ãƒ—ãƒã‚°ãƒ©ãƒ ã¯å¼·åˆ¶çµ‚äº†ã—ã¾ã™.
                              ! å¼•æ•° *err* ãŒä¸Žãˆã‚‰ã‚Œã‚‹å ´åˆ,
                              ! ãƒ—ãƒã‚°ãƒ©ãƒ ã¯å¼·åˆ¶çµ‚äº†ã›ãš, ä»£ã‚ã‚Šã«
                              ! *err* ã« .true. ãŒä»£å…¥ã•ã‚Œã¾ã™.
                              !
                              ! Exception handling flag.
                              ! By default, when error occur in
                              ! this procedure, the program aborts.
                              ! If this *err* argument is given,
                              ! .true. is substituted to *err* and
                              ! the program does not abort.

  !-----------------------------------
  !  ä½œæ¥å¤‰æ•°
  !  Work variables
  real, allocatable:: rvalue(:)
  integer:: siz, stat
!!$  integer:: i
  logical:: myerr
  integer:: out_unit
  integer:: indent_len
  character(STRING):: indent_str
  character(*), parameter:: subname = 'GTVarPutLine'
continue
  call BeginSub(subname, '%d', i=(/var % mapid/))
  stat = DC_NOERR
  !-----------------------------------------------------------------
  !  å‡ºåŠ›å…ˆè£…ç½®ç•ªå·ã¨å—ä¸‹ã’ã®è¨å®š
  !  Configure output unit number and indents
  !-----------------------------------------------------------------
  if ( present(unit) ) then
    out_unit = unit
  else
    out_unit = STDOUT
  end if

  indent_len = 0
  indent_str = ''
  if ( present(indent) ) then
    if ( len(indent) /= 0 ) then
      indent_len = len(indent)
      indent_str(1:indent_len) = indent
    end if
  end if

  !-----------------------------------------------------------------
  !  åˆæœŸè¨å®šã•ã‚Œã¦ã„ãªã„å¤‰æ•°ã®å°å—
  !  Print uninitialized variables
  !-----------------------------------------------------------------
  if ( var % mapid < 0 ) then
    call Printf( out_unit, &
      & indent_str(1:indent_len) // &
      & '#<GT_VARIABLE:: @initialized=%y>', &
      & l = (/.false./) )
    goto 999
  end if

  !-----------------------------------------------------------------
  !  åˆæœŸè¨å®šã•ã‚Œã¦ã„ã‚‹å¤‰æ•°ã®å°å—
  !  Print initialized variables
  !-----------------------------------------------------------------
  call Inquire(var, size=siz)
  call DbgMessage('size = %d', i=(/siz/))
  stat = DC_NOERR
  allocate(rvalue(siz), stat=stat)
  if (stat /= DC_NOERR) then
    stat = GT_ENOMEM
    goto 999
  endif
  call Get(var, rvalue, size(rvalue), err=myerr)
  if (myerr) then
    stat = ErrorCode()
    if (stat /= DC_NOERR) then
      call Printf( out_unit, &
        & indent_str(1:indent_len) // &
        & '#<GT_VARIABLE:: @initialized=%y>', &
        & l = (/.false./) )
      stat = DC_NOERR
    end if
    goto 999
  endif
  call Printf( out_unit, &
    & indent_str(1:indent_len) // &
    & '#<GT_VARIABLE:: @initialized=%y', &
    & l = (/.true./) )

  call PutLine( rvalue, unit = out_unit, &
    & lbounds = lbound(rvalue), &
    & ubounds = ubound(rvalue), &
    & indent = indent_str(1:indent_len) // &
    & ' @value=' )

!!$  do, i = 1, size(rvalue)
!!$    call Printf(fmt='%r', r=(/rvalue(i)/))
!!$  end do

  call Printf( out_unit, &
    & indent_str(1:indent_len) // &
    & '>' )

  deallocate(rvalue, stat=stat)
  if (stat /= DC_NOERR) stat = GT_ENOMEM

999 continue
  call StoreError(stat, subname, err)
  call EndSub(subname, '%d stat=%d', i=(/var % mapid, stat/))
end subroutine GTVarPutLine