[Numpy-discussion] [F2PY]: Allocatable Arrays

Pearu Peterson pearu@cens.ioc...
Fri Feb 1 08:45:09 CST 2008


On Fri, February 1, 2008 1:28 pm, Andrea Gavana wrote:
> Hi All,
>
>     I sent a couple of messages to f2py mailing list, but it seems
> like my problem has no simple solution so I thought to ask for some
> suggestions here.

Sorry, I haven't been around there long time.

> Basically, I read some huge unformatted binary files which contain
> time-step data from a reservoir simulation. I don't know the
> dimensions (i.e., lengths) of the vectors I am going to read, and I
> find out this information only when I start reading the file. So, I
> thought it would be nice to do something like:
>
> 1) Declare outputVector as allocatable;
> 2) Start reading the file;
> 3) Find the outputVector dimension and allocate it;
> 4) Read the data in the outputVector;

looks ok.

> 5) Return this outputVector.

What do you mean by "return"? You cannot return allocatable arrays
as far as comes to using f2py for generating wrappers. However,
you can access allocatable array outputVector if it is module data,
as you do below.

> It works when I compile it and build it in Fortran as an executable
> (defining a "main" program in my f90 module), but it bombs when I try
> to use it from Python with the error:
>
> C:\Documents and Settings\gavana\Desktop\ECLIPSEReader>prova.py
> Traceback (most recent call last):
>  File "C:\Documents and
> Settings\gavana\Desktop\ECLIPSEReader\prova.py", line 3, in <module>
>    inteHead, propertyNames, propertyTypes, propertyNumbers =
> ECLIPSEReader.init.readinspec("OPT_INJ.INSPEC")
> ValueError: failed to create intent(cache|hide)|optional array-- must
> have defined dimensions but got (-1,)

This exception is not directly related to what follows below.

> So, I have tried with a suggestion given in the f2py mailing list, and
> I found out that this routine works:
>
>
> MODULE DUMMY
> IMPLICIT NONE
>
> ! Ok, so I want an allocatable array as output
>
> real(8), allocatable :: realOutput(:)
...

> END MODULE DUMMY
>
>
> But this one doesn't work:
>
>
> MODULE DUMMY
> IMPLICIT NONE
>
> ! Ok, so I want an allocatable array as output
>
> real(8), allocatable :: realOutput(:)
> integer, allocatable :: inteOutput(:)
>
> CONTAINS
...
> END MODULE DUMMY

This one works fine here:

$ f2py -c -m m2 m2.f90 --fcompiler=gnu95
>>> import m2
>>> print m2.dummy.__doc__
realoutput - 'd'-array(-1), not allocated
inteoutput - 'i'-array(-1), not allocated
allocatedummy - Function signature:
  allocatedummy(dummyinput)
Required arguments:
  dummyinput : input int


> The difference between the 2 scripts, is just that in the second one I
> want 2 allocatable arrays instead of 1. When I compile it with f2py, I
> get this warning from getarrdims:
>
> Building modules...
>        Building module "dummy"...
>                Constructing F90 module support for "dummy"...
>                  Variables: realoutput inteoutput
                              ^^^^^^^^^^^^^^^^^^^^^

Looks like both allocatable arrays should be present also in your
case.

> getarrdims:warning: assumed shape array, using 0 instead of ':'
> getarrdims:warning: assumed shape array, using 0 instead of ':'

These warnings can be ignored.

> Which is not present if I compile script number 1. Actually, if I run
> script 2, I can't access anymore the 2 variables realoutput and
> inteoutput (they are not there), while with script 1 I can easily
> access realoutput by writing dummy.dummy.realoutput.

What do you mean by accessing? What happens when you type:
  dummy.dummy.inteoutput
?

Note that when you call AllocateDummy function, then you allocate
and then deallocate the arrays. So, in Python dummy.dummy.realoutput
and dummy.dummy.inteoutput should always return None.

See

http://cens.ioc.ee/projects/f2py2e/usersguide/index.html#allocatable-arrays

for how to use allocatable module data from Python.

> I can't actually see any big difference between the 2 scripts... am I
> missing something?
>
> This is Windows XP, Python 2.5, numpy 1.0.3.1, Compaq Visual Fortran
> 6.6, MS Visual Studio .NET 2003.

I am using numpy from svn.

Regards,
Pearu



More information about the Numpy-discussion mailing list