# Pass Fortran array of unknown size to Julia

**URL:** <https://discourse.julialang.org/t/pass-fortran-array-of-unknown-size-to-julia/74784>\
**Category:** General Usage\
**Tags:** fortran\
**Created:** [January 18, 2022, 6:33am UTC](https://discourse.julialang.org/t/pass-fortran-array-of-unknown-size-to-julia/74784 "2022-01-18T06:33:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)\
**Post date:** [January 18, 2022, 6:33am UTC](https://discourse.julialang.org/t/pass-fortran-array-of-unknown-size-to-julia/74784/1 "2022-01-18T06:33:54Z")

</div>

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

```julia
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

```fortran
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.

---

<div class="post-metadata">

**Author:** ![vavrines](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vavrines/32/8024_2.png) [@vavrines](https://discourse.julialang.org/u/vavrines)\
**Post date:** [January 18, 2022, 9:03am UTC](https://discourse.julialang.org/t/pass-fortran-array-of-unknown-size-to-julia/74784/2 "2022-01-18T09:03:31Z")

</div>

Seems to be no better way at the time I was doing the test.  
The Fortran subroutine

```julia
subroutine foo(w)
    real(kind=8), dimension(:), intent(in) :: w
end

```

has to be wrapped into

```julia
subroutine foo1(w, n)
    integer, intent(in) :: n
    real(kind=8), intent(in) :: w(n)
end

```

to be called from Julia.  
Wondering if there is an alternative now 😮‍💨

---

<div class="post-metadata">

**Author:** ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)\
**Post date:** [January 20, 2022, 1:12am UTC](https://discourse.julialang.org/t/pass-fortran-array-of-unknown-size-to-julia/74784/3 "2022-01-20T01:12:46Z")

</div>

I am expecting something like Fortran passes the size of the array and the pointer to the array to Julia, and the array can be reconstructed using the above information.
