Re: [f2py] Frotran, GDAL and f2py
Stiaan Gerber <14527839-/[email protected]>
| Newsgroups | gmane.comp.python.f2py.user |
|---|---|
| Message-ID | <[email protected]> |
You can call Python from Fortran, but f2py is not involved. I am doing it and it works rather well for me. Unfortunately, it can get a bit complicated. I have attached an example. Relevant documentation is The Python/C API , Extending and embedding Python and the c-api section from the Numpybook. It may seem like a lot of work, but for me it was worth it. And it's not that hard once you get the hang of it. Stiaan On 03/02/2010 02:27 AM, Robin wrote: > On Mon, Mar 1, 2010 at 11:21 PM, Jamie Lahowetz <[email protected]> wrote: > >> Thanks for the reply. Its nice to know that there are other packages for >> shapefile manipulation. GDAL is an opensource GIS package that does >> everything I need it to do so there is no real benifit in learning a new >> package. My first question still stands though: How do I call python from >> fortran using f2py? >> > I don't think you really can. f2py is designed to allow calling > fortran code from python as a python extension. I think there is a > facility for callbacks to python from the fortran code, but I don't > think that can be used in a standalone way (without calling the > fortran code as an extension from a python process). > > I think the idea is one can write the wrapping stuff (including file > io etc.) in Python which is faster to write but slower to run, and > then implement any computationally expensive inner loops in fortran > which are called easily from the python process. > > I don't know of any way to call python from a standalone fortran > program (other than through a shell). In C it is possible to embed a > python interpreter in another process, to use python from a c program, > but I am not aware of anything similar for fortran. Possibly you could > embed Python in your own C library and interact with that from fortran > but I think it would be quite complicated. > > Cheers > > Robin > > >> On Mon, Mar 1, 2010 at 2:59 PM, James Boyle <[email protected]> wrote: >> >>> As part of Jeff Whittaker's Basemap toolkit for matplotlib, there is a >>> ability to read/write shapefiles. >>> The ability to write shapefiles in not cited in the online docs, but there >>> is an example in the download. >>> I have only used the read so YMMV. >>> Jeff is a meteorologist and his stuff is very useful in that field. >>> http://matplotlib.sourceforge.net/ >>> http://matplotlib.sourceforge.net/users/toolkits.html >>> --Jim >>> On Mar 1, 2010, at 12:27 PM, Jamie Lahowetz wrote: >>> >>> I have a fortran program that creates storm tracks. I can output these >>> tracks into a csv file but it would be really helpful if I could just create >>> shapefiles instead. So heres my question(s): >>> 1. Im very new to f2py and from what I read, python can be called from >>> fortran using f2py. I looked at the examples in the documentation but dont >>> fully understand how to really do this. Can anyone help me out by maybe >>> explaining it or perhaps share an example of python being called from a >>> Fortran program. >>> 2. Is it even possible to do the above using the GDAL python bindings? >>> >>> -- >>> Jamie Ryan Lahowetz >>> University of Nebraska - Lincoln >>> Graduate Student - Geosciences >>> 402.304.0766 >>> [email protected] >>> _______________________________________________ >>> f2py-users mailing list >>> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected] >>> http://*cens.ioc.ee/mailman/listinfo/f2py-users >>> >>> >>> _______________________________________________ >>> f2py-users mailing list >>> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected] >>> http://cens.ioc.ee/mailman/listinfo/f2py-users >>> >>> >> >> >> -- >> Jamie Ryan Lahowetz >> University of Nebraska - Lincoln >> Graduate Student - Geosciences >> 402.304.0766 >> [email protected] >> >> _______________________________________________ >> f2py-users mailing list >> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected] >> http://cens.ioc.ee/mailman/listinfo/f2py-users >> >> >> > _______________________________________________ > f2py-users mailing list > f2py-users-Y4l6ocDipWCuvFJfX82//[email protected] > http://cens.ioc.ee/mailman/listinfo/f2py-users > _______________________________________________ f2py-users mailing list f2py-users-Y4l6ocDipWCuvFJfX82//[email protected] http://cens.ioc.ee/mailman/listinfo/f2py-users
glue.c
(text/x-c++src, 1.3 KB)
#include <python2.6/Python.h>
#include "/usr/lib64/python/site-packages/numpy/core/include/numpy/arrayobject.h"
#include <string.h>
void init_py_(char *pyfilename, int *pyfilename_len);
void py_foo_(double *args, int *nargs, double *result);
int initialized = 0;
PyObject *pymod, *dict;
PyObject *py_foo;
void init_py_(char *pyfilename, int *pyfilename_len) {
char pyexecstring[30] = "execfile('";
char *lastbit = "')\n";
if (!initialized) {
initialized = 1;
Py_Initialize();
import_array();
pyfilename[*pyfilename_len] = '\0';
strcat(pyexecstring, pyfilename);
strcat(pyexecstring, lastbit);
PyRun_SimpleString(pyexecstring);
pymod = PyImport_AddModule("__main__");
dict = PyModule_GetDict(pymod);
py_foo = PyDict_GetItemString(dict, "foo");
}
}
void py_foo_(double *args, int *nargs, double *result) {
PyObject *foo_args_array;
PyObject *foo_args;
PyObject *foo_result;
foo_args_array = PyArray_SimpleNewFromData(1, (void*)nargs, NPY_DOUBLE, args);
foo_args = PyTuple_Pack(2, foo_args_array, PyInt_FromLong(*nargs));
foo_result = PyObject_CallObject(py_foo, foo_args);
if(!PyArg_ParseTuple(foo_result, "d", result)) {
printf("Error: ParseTuple failed.\n");
}
}
main.f95
(text/plain, 359 B)
program main character*25 pyfname integer pyfname_len double precision args(2), result integer nargs pyfname = "pylib.py" pyfname_len = 8 write (*,"(A)") "Initializing Python" call init_py(pyfname,pyfname_len) args(1) = 2d0 args(2) = 3.5 nargs = 2 write (*,"(A)") "Calling Python" call py_foo(args, nargs, result) write (*,"(F4.2)") result end program
Makefile
(text/plain, 180 B)
py2f: glue.o main.o gfortran -lpthread -ldl -lutil -lm -lpython2.6 -o py2f glue.o main.o main.o: main.f95 gfortran -c main.f95 -o main.o glue.o: glue.c gcc -c glue.c -o glue.o
pylib.py
(text/x-python, 135 B)
#!/usr/bin/python
def foo(args, nargs):
for val in args:
print val
print nargs
return (args[0]*args[1],)