[Numpy-discussion] [F2PY]: Allocatable Arrays

Andrea Gavana andrea.gavana@gmail....
Fri Feb 1 05:28:45 CST 2008


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.

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;
5) Return this outputVector.

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,)


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(:)

CONTAINS

       subroutine AllocateDummy(dummyInput)

               implicit none
               save

               ! dummyInput is *not* used, it's here just as
               ! an example
               integer, intent(in) :: dummyInput

               ! Allocate and build the output array
               allocate(realOutput(10))

               realOutput(1:10) = 0.0
               realOutput(3) = 3.0
               realOutput(7) = 7.0

               deallocate(realOutput)

               return

       end subroutine AllocateDummy


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

       subroutine AllocateDummy(dummyInput)

               implicit none
               save

               ! dummyInput is *not* used, it's here just as
               ! an example
               integer, intent(in) :: dummyInput


               ! Allocate and build the output array
               allocate(realOutput(10))
               allocate(inteOutput(20))

               realOutput(1:10) = 0.0
               realOutput(3) = 3.0
               realOutput(7) = 7.0

               inteOutput(10) = 2

               deallocate(realOutput)
               deallocate(inteOutput)

               return

       end subroutine AllocateDummy


END MODULE DUMMY



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
getarrdims:warning: assumed shape array, using 0 instead of ':'
getarrdims:warning: assumed shape array, using 0 instead of ':'
                       Constructing wrapper function "dummy.allocatedummy"...
                         allocatedummy(dummyinput)


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.
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.

Thank you for all your suggestions, I am at loss.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/


More information about the Numpy-discussion mailing list