C+ C NAME: C iGetLun C PURPOSE: C Handle pool of logical numbers for opening files to avoid problems C with using the same logical number to open different files. C CALLING SEQUENCE: function iGetLun(cFile) C INPUTS: C cFile character*(*) file name C OUTPUTS: C iGetLun: integer assigned logical unit number; FIL__NOUNIT is returned C if no unit numbers are available C INCLUDE: include 'filparts.h' C RESTRICTIONS: C Only logical unit numbers in range [LUMin,LUMax]=[30,40] are processed. C PROCEDURE: C > In general files should be opened using bOpenFile, C which automatically assigns a unit number. C > A convenient way to release a unit number iU is by C iU = iFreeLun(iU) C This sets iU to FIL__NOUNIT, i.e. an 'invalid' unit number. C > An internal array LUStatus is maintained with elements set to C 0 (LU available) or 1 (LU in use). C MODIFICATION HISTORY: C ?, Paul Hick (UCSD/CASS; pphick@ucsd.edu) C- parameter (LUMin=30) parameter (LUMax=40) parameter (LUTot=LUMax-LUMin+1) integer LUStatus(LUMin:LUMax) /LUTot*0/ save LUStatus integer LUScratch(LUMin:LUMax) /LUTot*0/ save LUScratch character LUName(LUMin:LUMax)*(FIL__LENGTH) /LUTot*' '/ save LUName character cFile*(*) iGetLun = LUMin do while (iGetLun .le. LUMax .and. LUStatus(iGetLun) .eq. 1) iGetLun = iGetLun+1 end do if (iGetLun .gt. LUMax) then iGetLun = FIL__NOUNIT else LUStatus (iGetLun) = 1 LUScratch(iGetLun) = 0 LUName (iGetLun) = cFile end if return C+ C NAME: C iScratchLun C PURPOSE: C Mark unit number as 'scratch file' C CALLING SEQUENCE: entry iScratchLun(LU) C INPUTS: C LU integer (read-only) logical unit number to be processed C OUTPUTS: C LU integer LU C RESTRICTIONS: C Only logical unit numbers in range [LUMin,LUMax]=[30,40] are processed. C PROCEDURE: C See href=iGetLun= C Marking the unit number as 'scratch file' will result in deleting the C file when the logical unit number is released using iFreeLun (even if C argument iU is positive). Note that scratch files can be cleaned up C by calling iFreeAllLun before aborting. C MODIFICATION HISTORY: C JUN-2004, Paul Hick (UCSD/CASS; pphick@ucsd.edu) C- if (LU .ne. FIL__NOUNIT .and. LUMin .le. LU .and. LU .le. LUMax) then if (LUStatus(LU) .eq. 1) LUScratch(LU) = 1 end if iScratchLun = LU return C+ C NAME: C iFreeLun C PURPOSE: C Release specified logical unit number C CALLING SEQUENCE: entry iFreeLun(LU) C INPUTS: C LU integer (read-only) logical unit number to be processed C a negative LU will attempt to close C and delete the file (close(LU,status='DELETE') C For positive LU the file is closed: close(LU) C OUTPUTS: C LU integer always returns FIL__NOUNIT C RESTRICTIONS: C Only logical unit numbers in range [LUMin,LUMax]=[30,40] are processed. C PROCEDURE: C See href=iGetLun= C MODIFICATION HISTORY: C ?, Paul Hick (UCSD/CASS; pphick@ucsd.edu) C- i = abs(LU) if (i .ne. FIL__NOUNIT .and. LUMin .le. i .and. i .le. LUMax) then if (LUStatus(i) .eq. 1) then if (LU .lt. 0 .or. LUScratch(i) .eq. 1) then close (i,iostat=iFreeLun,status='DELETE') LUScratch (i) = 0 else close (i,iostat=iFreeLun) end if LUStatus(i) = 0 end if end if iFreeLun = FIL__NOUNIT ! LU = iFreeLun(LU) invalidates LU return C+ C NAME: C iFreeAllLun C PURPOSE: C Releases all logical units. C CALLING SEQUENCE: entry iFreeAllLun() C INPUTS: C (none) C OUTPUTS: C (none) C RESTRICTIONS: C Only logical unit numbers in range [LUMin,LUMax]=[30,40] are C processed. C PROCEDURE: C See href=iGetLun= C MODIFICATION HISTORY: C ?, Paul Hick (UCSD/CASS; pphick@ucsd.edu) C- do i=LUMin,LUMax if (LUScratch(i) .eq. 1) then close (i, iostat=iFreeAllLun, status='DELETE') LUScratch (i) = 0 else close (i, iostat=iFreeAllLun) end if LUStatus(i) = 0 end do iFreeAllLun = FIL__NOUNIT return C+ C NAME: C iListAllLun C PURPOSE: C List the status of logical unit (mainly used for debugging). C CALLING SEQUENCE: entry iListAllLun() C INPUTS: C (none) C OUTPUTS: C (none) C RESTRICTIONS: C Only logical unit numbers in range [LUMin,LUMax]=[30,40] are processed. C PROCEDURE: C See href=iGetLun= C MODIFICATION HISTORY: C ?, Paul Hick (UCSD/CASS; pphick@ucsd.edu) C- n = 0 do i=LUMin,LUMax if (LUStatus(i) .eq. 1) n = n+1 end do write (*,*) '# open units: ',n,'/',LUMax-LUMin+1 do i=LUMin,LUMax j = itrim(LUName(i)) if (LUStatus(i) .eq. 1 .or. j .gt. 0) write (*,'(I2,3X,A)') LUStatus(i),LUName(i)(:j) end do iListAllLun = LUMax-LUMin+1-n return end