I have an array that is read from the fortran routine as a 1D array via f2py. Then in python this array changes:
a=np.zeros(nx*ny*nz) read_fortran_array(a) a=a.reshape(nz,ny,nx)
Now I would like to pass this array back to fortran as a 3D array.
some_data=fortran_routine(a)
The problem is that f2py keeps trying to transpose a before passing fortran_routine. The fortran procedure is as follows:
subroutine fortran_routine(nx,ny,nz,a,b) real a real b integer nx,ny,nz !f2py intent(hidden) nx,ny,nz !f2py intent(in) a !f2py intent(out) b ... end subroutine
How to prevent all carry back and forth? (I am completely happy to use the various bilingual array indexing conventions).
EDIT
It seems that np.asfortranarray
or np.flags.f_contiguous
should have some part in the solution, I just can not figure out which part (or maybe ravel
and then reshape(shape,order='F')
?
EDIT
This message seems to have caused some confusion. The problem here is that f2py
trying to save the indexing scheme instead of the memory layout. So, if I have a numpy array (in C order) with the form (nz, ny, nx)
, then f2py tries to force the array to have the form (nz, ny, nx)
in fortran too. If f2py kept the memory layout, the array would have the form (nz, ny, nx)
in python and (nx, ny ,nz)
in fortran. I want to save the memory layout.