nbren12 / call_py_fort

Call python from fortran
81 stars 22 forks source link

Mismatched dimensinons and type #26

Open mankoff opened 1 year ago

mankoff commented 1 year ago

I'm trying to pass an array between Fortran and Python but the dimensions and type are not matched. In Fortran I have

(gdb) ptype tgrnd
type = real(kind=8) (2,72,0:47)

And I pass it with your library with:

call set_state("tg", tgrnd)
call call_function("foo", "bar")

And in Python the type and dimensions are:

def bar(STATE):
    tg = STATE.get("tg", np.nan)
    print("LIME_py0:", tg.shape)
    print("LIME_py0:", type(tg))
    print("LIME_py0:", type(tg[0,0,0]))

Which outputs

LIME_py0: (25, 72, 2)
LIME_py0: <class 'numpy.ndarray'>
LIME_py0: <class 'numpy.float64'>

The suspicious part above is the dimensions are 0:47 and not 48. This array, tgrnd, is created with the following statement:

      REAL*8, DIMENSION(2,GRID%I_STRT_HALO:GRID%I_STOP_HALO,
     &                    GRID%J_STRT_HALO:GRID%J_STOP_HALO) ::
     &     TGRND,TGRN2,TGR4,E1 ! 2 for ocean,seaice

I have a work-around: I can create a new array with explicit dimensions, then copy the values to that, e.g.,

real*8, dimension(2,72,48) :: LIME_T
LIME_T = tgrnd

And then if I pass LIME_T to set_state, everything works. I'm not yet sure how to fix this issue, but wanted to raise it.

nbren12 commented 1 year ago

I wouldn't be surprised if there is some error related to using lbound!=1. This is the relevant portion of the code: https://github.com/nbren12/call_py_fort/blob/b5f1ac561128c7920cf901c9274407b21ba8e706/src/callpy_mod.f90#L255.