F2PY changing integers to arrays???

Uwe Schmitt rocksportrocker at googlemail.com
Tue Sep 9 21:50:45 CEST 2008


On 9 Sep., 18:41, john <john.m.ro... at gmail.com> wrote:
> I have a simple module which performs basic operations on plot3d files
> (below).  I wrapped like:
>
> f2py --fcompiler=gfortran -m plot3d -c prec.f90 plot3d.f90
>
> That seems to work fine, but i get some unexpected results...
>
> >>> from plot3d import plot3d as p3
> >>> dir(p3)
>
> ['imax', 'jmax', 'kmax', 'mg', 'prc', 'printall', 'readit', 'writeit',
> 'writeit2d']>>> p3.readit( "mesh.xrtz.dat", "FORMATTED", ".TRUE." )
> >>> p3.imax
>
> array(409)
>
> "409" is correct, but "imax" is declared as an INTEGER in fortran and
> now it's an array in python???  Any ideas?
>
> # prec.f90
> MODULE prec
> IMPLICIT NONE
> INTEGER, PARAMETER :: single = SELECTED_REAL_KIND(p=6,r=37)
> INTEGER, PARAMETER :: double = SELECTED_REAL_KIND(p=15,r=200)
> END MODULE prec
>
> # plot3d.f90
> MODULE PLOT3D
> USE prec
> IMPLICIT NONE
> INTEGER, PARAMETER :: prc=single
> REAL(prc), ALLOCATABLE, DIMENSION(:,:,:,:) :: x, y, z
> INTEGER :: mg, imax, jmax, kmax
>
> CONTAINS
>
> !----------
>     SUBROUTINE READIT( fname, ftype, fmg )
>     ! reads the plot3d, formatted, mg file in xyz
>     IMPLICIT NONE
>     CHARACTER(len=20), INTENT(IN) :: fname, ftype
>
>     LOGICAL :: fmg
>     INTEGER :: i, j, k, n, f=1
>
>     SELECT CASE (ftype)
>         CASE ('FORMATTED')
>             OPEN( UNIT=f, FILE=fname, STATUS='OLD', ACTION='READ',
> FORM=ftype )
>             IF (fmg) READ(f,*) mg ! only read if multigrid
>             READ(f,*) imax, jmax, kmax
>             ALLOCATE( x(mg, imax, jmax, kmax) )
>             ALLOCATE( y(mg, imax, jmax, kmax) )
>             ALLOCATE( z(mg, imax, jmax, kmax) )
>             READ(f,*) ((((x(n,
> i,j,k),i=1,imax),j=1,jmax),k=1,kmax),n=1,mg), &
>                       ((((y(n,
> i,j,k),i=1,imax),j=1,jmax),k=1,kmax),n=1,mg), &
>                       ((((z(n,
> i,j,k),i=1,imax),j=1,jmax),k=1,kmax),n=1,mg)
>         CASE ('UNFORMATTED')
>             OPEN( UNIT=f, FILE=fname, STATUS='OLD', ACTION='READ',
> FORM=ftype )
>             IF (fmg) READ(f) mg ! only read if multigrid
>             READ(f) imax, jmax, kmax
>             ALLOCATE( x(mg, imax, jmax, kmax) )
>             ALLOCATE( y(mg, imax, jmax, kmax) )
>             ALLOCATE( z(mg, imax, jmax, kmax) )
>             READ(f) ((((x(n,
> i,j,k),i=1,imax),j=1,jmax),k=1,kmax),n=1,mg), &
>                     ((((y(n,
> i,j,k),i=1,imax),j=1,jmax),k=1,kmax),n=1,mg), &
>                     ((((z(n,
> i,j,k),i=1,imax),j=1,jmax),k=1,kmax),n=1,mg)
>         CASE DEFAULT
>             WRITE(*,*) 'filetype not supported in <PLOT3D.READIT>'
>             STOP
>     END SELECT
>
>     CLOSE(f)
>
>     END SUBROUTINE READIT
>
> END MODULE PLOT3D

You can generate the according .pyf file with some command line flag I
forgot.
There you can see what the parser derived from the fortran code and
you
can edit this file for further configuration of the generated python
module.

Greetings, Uwe



More information about the Python-list mailing list