Reading Fortran data using python interface
Created by: germasch
When reading data written by a Fortran code using python, indices end up being reversed:
Sample Fortran code (it's based on the included heatmap test)
program TestBPWriteReadHeatMap2D
use mpi
use adios2
implicit none
type(adios2_adios) :: adios
type(adios2_io) :: ioPut
type(adios2_engine) :: bpWriter
type(adios2_variable) :: var
real(kind=8) :: temperature(0:4, 0:9)
integer(kind=8), parameter :: ishape(2) = (/5, 10/)
integer(kind=8), parameter :: istart(2) = (/0, 0/)
integer :: ierr
call mpi_init(ierr)
call adios2_init(adios, MPI_COMM_WORLD, adios2_debug_mode_on, ierr)
call random_number(temperature)
print*, 'sample temperature(2,8) =', temperature(2, 8)
call adios2_declare_io(ioPut, adios, 'HeatMapWrite', ierr)
call adios2_define_variable(var, ioPut, 'temperature', adios2_type_dp, &
2, ishape, istart, ishape, &
adios2_constant_dims, ierr)
call adios2_open(bpWriter, ioPut, 'HeatMap2D_f.bp', adios2_mode_write, &
ierr)
call adios2_put(bpWriter, var, temperature, ierr)
call adios2_close(bpWriter, ierr)
call adios2_finalize(adios, ierr)
call mpi_finalize(ierr)
end program TestBPWriteReadHeatMap2D
Output:
[kai@macbook build-fortran (pr/fix-mgard *)]$ bin/test_fortran
sample temperature(2,8) = 0.13062623044039723
Reading the bp file from Python:
import adios2
fh = adios2.open("/Users/kai/build/ADIOS2/build-fortran/HeatMap2D_f.bp", "r")
t = fh.read('temperature')
print(t[2, 8])
gives
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-10-cf0fcf0017a8> in <module>
2 fh = adios2.open("/Users/kai/build/ADIOS2/build-fortran/HeatMap2D_f.bp", "r")
3 t = fh.read('temperature')
----> 4 print(t[2, 8])
IndexError: index 8 is out of bounds for axis 1 with size 5
To access the Fortran data element t(2, 8)
from Python, one has to reverse the index order:
>>> print(t[8, 2])
0.13062623044039723
This is related to #1661 , but actually fixable without API additions.