F2PY changing integers to arrays???

J

john

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...
['imax', 'jmax', 'kmax', 'mg', 'prc', 'printall', 'readit', 'writeit',
'writeit2d']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
 
M

mwojc

john said:
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...
['imax', 'jmax', 'kmax', 'mg', 'prc', 'printall', 'readit', 'writeit',
'writeit2d']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

I'm not sure why you obtain array(409) instead 409, but you can use this
type in python as normal integer. For example array(409)+409 gives integer
value 818.

Greetings
 
J

john

Hmmm... I didn't even try that... I thought this might have caused
problems down the road passing this var to other classes, etc., but i
guess not!

Thanks.
 
U

Uwe Schmitt

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

['imax', 'jmax', 'kmax', 'mg', 'prc', 'printall', 'readit', 'writeit',
'writeit2d']>>> p3.readit( "mesh.xrtz.dat", "FORMATTED", ".TRUE." )
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top