subroutine GTDevOpen(dev, ws_id, err)
    use gtgraph_types, only: GT_DEVICE
    use gtgraph_com, only: gtdev_wsn_default
    use netcdf_f77, only: NF_EINVAL
    use dc_error, only: StoreError
    use sysdep, only: AbortProgram
    use dcl, only: DclOpenGraphics, DclSelectDevice
    use dc_trace, only: dbg_scratch, debug, dbg
implicit none
    type(GT_DEVICE), intent(out):: dev
    integer, intent(in), optional:: ws_id
    logical, intent(out), optional:: err
    integer, save:: opened_wsn = 0
    integer:: wsn
continue
    wsn = gtdev_wsn_default
    if (present(ws_id)) wsn = ws_id
    if (opened_wsn /= 0) then
        call StoreError(NF_EINVAL, 'GTDevOpen', err=err, cause_i=wsn)
        return
    else
        if (present(err)) err = .FALSE.
    endif
    if (wsn == 0) then
        !-- コンソール依存コード。いずれ選択化 --!
        wsn = DclSelectDevice()
        gtdev_wsn_default = wsn
    endif
    if (wsn == 3) call dbg_scratch(.true.)
    if (debug()) then
        call glistx('MSGUNIT', dbg)
    else
        call glistx('MSGLEV', 2)
    endif
    call DclOpenGraphics(wsn)
    dev%ws_id = wsn
    opened_wsn = wsn
end subroutine

subroutine GTDevClose(dev)
    use gtgraph_types, only: GT_DEVICE
    use dcl, only: DclCloseGraphics
    use dc_trace, only: dbg_scratch
implicit none
    type(GT_DEVICE), intent(inout):: dev
    integer:: iu
continue
    call DclCloseGraphics()
    call gliget('IOUNIT', iu)
    call glistx('MSGUNIT', iu)
    call dbg_scratch(.false.)
    dev%ws_id = 0
end subroutine
