# newunit Function

## public function newunit() result(nu)

Helper function to provide new unit, as long as F2008 newunit argument in open statement is not commonly available. To be used right in front of the open statement like this: myUnit = newunit() open(myUnit, ...)

None

## Source Code

  function newunit() result(nu)
! ---------------------------------------------------------------------------
! Melven Zoellner:
! for the solspecunit and in other places the unit is not kept open,
! so this does only work correctly if we count up over different calls
! to this function, currently not thread save...
!HK: Sorry this is not the task of newunit, it is supposed to serve
!HK: as an replacement for the new_unit keyword in the open statement
!HK: of Fortran, which will definitely not keep track of the units
!HK: opened so far. Also for iterative calls to newunit, you could
!HK: reach the limit of integers easily!
!HK: If you need to keep track of a file over closes and subsequent
!HK: reopenings, you need to do this in some other way!
integer :: nu
! ---------------------------------------------------------------------------
integer, parameter :: nu_start = 22
logical :: connected
! ---------------------------------------------------------------------------

nu = nu_start
inquire(unit=nu, opened=connected)
do while(connected)
nu = nu + 1
inquire(unit=nu, opened=connected)
end do
end function newunit