Hello! I have a question regarding calling a FORTRAN shared library that needs to read a binary file that is big-endian.
This is the file in question: https://github.com/wrf-model/WRF/blob/master/run/RRTMG_LW_DATA.
I can write some fortran to read the file:
content = """
real(kind=selected_real_kind(12)) :: fracrefao(16)
OPEN(10,FILE='RRTMG_LW_DATA', &
FORM='UNFORMATTED',STATUS='OLD')
READ (10) fracrefao
WRITE (*, *) fracrefao
"""
and then put it in a program:
prog = "program p\n" * content * "\nend program p\n"
open("read_file.f90", "w") do f
write(f, prog)
end
and compile and run it:
run(`gfortran -O2 -fconvert=big-endian read_file.f90 -o read_file.exe`)
run(`./read_file.exe`)
This all works fine, but only if I use -fconvert=big-endian
to do big-endian IO. If I don’t use that flag, I get this error:
At line 5 of file read_file.f90 (unit = 10, file = 'RRTMG_LW_DATA')
Fortran runtime error: End of file
What I really want to do, however, is to call a fortran shared library from julia to do the reading:
mod = """module m
contains
subroutine readtxt
""" * content * """
end subroutine readtxt
end module m
"""
open("read_file.f90", "w") do f
write(f, mod)
end
run(`gfortran -shared -O2 -fPIC -fconvert=big-endian read_file.f90 -o read_file.so`)
However, when I call the library:
ccall((:__m_MOD_readtxt, "./read_file.so"), Cvoid, ())
I get the error:
At line 7 of file read_file.f90 (unit = 10, file = 'RRTMG_LW_DATA')
Fortran runtime error: End of file
This is the same error that I get above when I don’t use -fconvert=big-endian
, so my suspicion is that somehow that flag is not being honored when I call it from Julia. Additionally, if I use a text file instead of a big-endian binary file, calling the shared library also works fine, which further supports the idea that the problem has something to do with binary endianness.
Does anyone know how I can fix this? Thanks!
-Chris