module lmdz_new_unit IMPLICIT NONE contains ! Returns an existing unit id that isn't already opened SUBROUTINE new_unit(unit) INTEGER, INTENT(OUT):: unit LOGICAL opened, exist unit = 0 DO INQUIRE(unit=unit, opened=opened, exist=exist) IF (exist .AND. .NOT. opened) RETURN unit = unit + 1 END DO END SUBROUTINE new_unit end module lmdz_new_unit