0

Currently I am running an atmospheric model which was written by Fortran 90. In some subroutines I made some innovations using python (mainly tensorflow). Now I need to implement this python code into Fortran, so is there any wrapper to do this? just calling python within a Fortran subroutine? I knew that F2py is able to wrap the fortran code into python script, i.e calling fortran subroutine in a python script, but I want to do the inverse way. I searched internet but couldn't find a way to do that.

Here is the pseudo code of my thoughts:

SUBROUTINE demo(INPUTS, OUTPUTS)
! Definition of INPUTS
REAL, DIMENSION(:,:,:,:), INTENT(IN) :: PINPUTS
REAL, DIMENSION(:,:,:,:), INTENT(IN) :: POUTPUTS

PINPUTS = INPUTS

! using some python wrappers
POUTPUTS = python_wrapper(PINPUTS)

OUTPUTS = POUTPUTS
END SUBROUTINE demo

And in the python script "python_wrapper.py", I need to do:

import tensorflow as tf

# read saved model "model_saved";
# get the variables "OUTPUTS" passed from demo.f90;

prediction = model_saved.predict(OUTPUTS)

# then pass this variable "prediction" back to Fortran subroutine demo.f90;
# so that the "prediction" will be used for following calculation of the atmospheric model

Note it is impossible to rewrite the atmospheric model (originally in Fortran 90 codes) into python codes, reinventing this will take me a lot of time. And also it's impossible to running all parts (including atmospheric model and the innovation part of python script) into a python script because the demo.f90 subroutine is just a tiny branch of the whole Fortran 90 atmospheric model.

So is there any package to do that? Thanks a lot! Thanks a lot!

12
  • There are many possible ways to interoperate Fortran with Python or C. I suggest you take a look at this relevant post here and ask the Fortran community there for help too: fortran-lang.discourse.group/t/… Commented May 31, 2021 at 18:49
  • Are you aware how the C interface to Python works? Commented May 31, 2021 at 18:53
  • 1
    @King That is about the inverse (and much easier) direction. Commented May 31, 2021 at 18:55
  • 1
    See stackoverflow.com/questions/17075418/… You are VERY unlikely to be able to do it yourself without some reasonable C or C++ knowledge. But you might find that some of the libraries mentioned there, like Forpy, there will be able to do it automatically for you. Commented May 31, 2021 at 19:08
  • 1
    Tensorflow can be used from C++ tensorflow.org/api_docs/cc and C++ can be called from Fortran using the Fortran/C interoperability in Fortran 2003. It is more direct than calling tf from Python. Commented May 31, 2021 at 19:11

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.