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!