Hi Hernan and Lucas
On Sat, Jan 23, 2021 at 12:35 PM Hernan Arango <arango@xxxxxxxxxxxxxxxxxx>
wrote:
> Given ONLY the NetCDF dataset filename, is there a function that can be
> used in a Fortran code (or a C-binding routine) to inquire if the filename
> is open (with a logical true or false result).
>
You can use Linux's command "lsof" to see if the file is already opened or
not:
call execute_command_line("lsof yourdatafile.nc", exitstat=i)
if (i == 0) then ! it's opened.
because lsof returns a nonzero integer on error and error includes the case
where the file is not opened by any processes.
But, there are other reasons for error. To make your program robust, you
would need to read the manual of lsof or to examine the output of the lsof
command like so
call execute_command_line("lsof yourdatafile.nc > output.txt 2>&1",
existstat=i)
open(newunit=uni, file="output.txt", . . .)
! . . . examine the contents of output.txt . . .
(I wish Fortran had a facility like popen() so that you could examine the
output more easily.)
If true, return the one or more identifiers (vector) associated with the
> opening of that specific filename. Notice that It is possible that such
> filename has been opened more than once and not closed.
>
lsof would show all processes that have opened the file, except that you
might encounter permission problems: you may not be allowed to know about
processes not owned by you. (The same problem should occur with /proc/*/fd
.)
Hope this helps.
Ryo