! GTDataTmpNam - 変数名の自動作成
! Copyright (C) GFD Dennou Club, 2000.  All rights reserved.

subroutine GTDataTmpNam(file, base, result)
    use dc_types, only: string
    use dc_url, only: UrlMerge
    use gtdata_generic, only: Open, Close
    use gtdata_types, only: GT_VARIABLE
implicit none
    character(len = *), intent(in):: file
    character(len = *), intent(in):: base
    character(len = *), intent(out):: result
    type(GT_VARIABLE):: var
    integer:: n
    logical:: failed
    character(len = string):: fnam, vnam, vnambase
continue
    if (file == "") then
        fnam = "gtool.nc"
    else
        fnam = file
    endif
    if (base == "") then
        vnambase = "tmp"
    else
        vnambase = base
    endif
    n = 0
    do
        n = n + 1
        ! compatibility note (2001-12-02, TOYODA Eizi)
        ! dc_string の toChar を使いたいのだが FQS Fortran が失敗する
        write(unit=vnam, fmt="(i32)") n
        vnam = trim(vnambase) // trim(adjustl(vnam))
        result = UrlMerge(file=fnam, var=vnam)
        call Open(var, result, writable=.FALSE., err=failed)
        if (failed) return
        call Close(var)
    enddo
end subroutine
