I need to read some binary data (a few arrays, stored in, say, WS2.HSX
) that is handled by a Fortran module. I would like to call the Fortran module from Julia. However, the sizes of the arrays are not known in advance.
Right now, my solution is to read the data twice: the first time the sizes of the arrays are returned to julia and the second time to actually read the data. A simple example would be
dimensions = zeros(Int32, 6)
ccall(
(:julia_interface_mp_get_dimensions_, "./julia_interface.so"),
Cvoid,
(Ref{Int32},),
dimensions
)
nspin = dimensions[5]
nh = dimensions[6]
hamiltonian = zeros(Float64, nh, nspin)
ccall(
(:julia_interface_mp_read_hsx_file_jl_, "./julia_interface.so"),
Cvoid,
(Ref{Float64}, Ref{Int32}, Ref{Int32}),
hamiltonian, nh, nspin
)
# There are many other arrays
The Fortran subroutines are
subroutine get_dimensions(dimensions)
integer, dimension(6), intent(out) :: dimensions
type(hsx_t) :: hsx
call read_hsx_file(hsx, "WS2.HSX")
dimensions(1) = hsx%nspecies
dimensions(2) = hsx%na_u
dimensions(3) = hsx%no_u
dimensions(4) = hsx%no_s
dimensions(5) = hsx%nspin
dimensions(6) = hsx%nh
end subroutine get_dimensions
subroutine read_hsx_file_jl(hamilt, nh, nspin)
real(dp), dimension(nh, nspin), intent(out) :: hamilt
type(hsx_t) :: hsx
integer, intent(in) :: nh
integer, intent(in) :: nspin
call read_hsx_file(hsx, "WS2.HSX")
hamilt = hsx%hamilt
end subroutine read_hsx_file_jl
This works, but I am wondering whether there is a better or more elegant solution.