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!

Xu Shan
  • 175
  • 3
  • 11
  • 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: https://fortran-lang.discourse.group/t/interfacing-fortran-code-from-python/1280 – Scientist May 31 '21 at 18:49
  • Are you aware how the C interface to Python works? – Vladimir F Героям слава May 31 '21 at 18:53
  • 1
    @King That is about the inverse (and much easier) direction. – Vladimir F Героям слава May 31 '21 at 18:55
  • Be aware that it is almost surely impossible in Fortran 90, it is way too old and obsolete. Fortran 2003 techniques will be necessary. Most codes that claim to be Fortran 90 are strictly speaking something else anyway. – Vladimir F Героям слава May 31 '21 at 18:56
  • Also, can't you actually use the C++ interface to tf instead? That is much closer to Fortran in comparison to Python. – Vladimir F Героям слава May 31 '21 at 18:58
  • Hi @VladimirF, the best solution I could imagine, is saving data in the Fortran subroutine, then call the python script without passing the ```OUTPUTS``` variable. And then within the python script the saved data will be loaded to perform the calculation of tf. Then the tf results ```prediction``` are saved again, and within demo.f90 it will be read, and then be used for the further calculation of the atmospheric model. I think it's quite stupid, so I posted the question here to ask for a solution.... – Xu Shan May 31 '21 at 19:05
  • For C, I imagined that the potential solution could be via C, or something about C++. But I only learned some basic knowledge about C when I was freshman in my undergraduate university...that's about 8 years ago...so maybe it impossible for me to do that... – Xu Shan May 31 '21 at 19:07
  • 1
    See https://stackoverflow.com/questions/17075418/embed-python-into-fortran-90 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. – Vladimir F Героям слава May 31 '21 at 19:08
  • @VladimirF, I don't know what you meant by "the C++ interface to tf instead"...the atmospheric model is written by Fortran 90 while the innovation is made by tf....so you mean it's like that I passed the variable following Fortran->C++->python, then after tf calculation, it is passed back from python to C++ to Fortran? – Xu Shan May 31 '21 at 19:09
  • 1
    Tensorflow can be used from C++ https://www.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. – Vladimir F Героям слава May 31 '21 at 19:11
  • OK, I will try Forpy first, if it does not work I will come back again. Thanks for your help! – Xu Shan May 31 '21 at 19:13
  • @XuShan have you figured out this question? – Zhendong Cao Aug 02 '22 at 16:35

0 Answers0