Reputation: 43
I have a FORTRAN program that is called from a Python script (as an ArcGIS tool). I need to pass an array, Raster_Z(i,j), from FORTRAN to python.
I have read about the Python subprocess module; however, I have not had much luck in understanding how to do this with FORTRAN. All examples I have found involve simple unix command line calls and not actual programs.
Has anyone had any experience in passing a variable from FORTRAN to Python via a memory PIPE?
Thank you for your time.
Upvotes: 3
Views: 1314
Reputation: 1188
Are you passing data /into/ fortran too, or just out? If possible, I would recommend against pipes, definitely for input into fortran, and if you are using the intel compiler. Check out my answer to this question: https://stackoverflow.com/a/10103339/1024514
A temporary file will probably work better -- if it is read just after writing the data will likely come from the OS buffer anyway. Or write a wrapper to call a fortran function from python to use the array in place -- I have not done that in python though.
Upvotes: 0
Reputation: 48725
To pass an array to FORTRAN through a PIPE from the subprocess
module, you will need to figure out how you would pass this array to FORTRAN as a command line argument. This is because PIPE uses a shell-like interface (or the actual shell if you want) to call the programs. To use your approach, I would make a python list that looked like this:
arguments = [rows, columns] + [x for x in Raster_Z.flatten()]
where I have assumed you are using numpy. This list can be PIPE'd to STDIN. This way, you tell FORTRAN the number of rows and columns of the array so it can allocate the memory, then it will read in the flattened array which you can reshape however you see fit. Flattening the array has the added benefit of removing the pesky conversion from row-order to column order storage of the array.
I have actually done exactly what you are looking to do, using PIPE, except that instead of piping the arrays to the FORTRAN routine through STDIN, I wrote it to a temporary file and then read it in from FORTRAN through the file. I had originally tried to do what I suggested just suggested above, but I ran into issues with FORTRAN reading large sets of data from STDIN... I attributed this to buffering. Your milage may vary and STDIN may work for you.
OR:
You could wrap your FORTRAN routine using f2py, and then send the array to FORTRAN directly as if it were a python function, no PIPEing needed!.
Upvotes: 1