1

I am attempting to wrap some fortran using python. This will ultimately involve the manipulation of strings and character arrays, so I thought I would try practicing on some simple stuff.

I am using gfortran 4.8.1, numpy 1.9.2, and python 3.4.3 on Windows 8

I created the following fortran code.

  SUBROUTINE TESTER(my_string,a,b,c,d)
  integer a,b,c,d
  character(LEN=1000) :: my_string
C stuff for f2py. 
Cf2py character, intent(in, out) :: my_string
Cf2py integer, intent(in, out) :: a
Cf2py integer, intent(in, out) :: b
Cf2py integer, intent(in, out) :: c
Cf2py integer, intent(in, out) :: d
  a = 1
  b = 2
  c = 3
  d = 4
  write(6,*) 'my_string = ',TRIM(my_string)
  my_string = TRIM(my_string)//'. I was added!'
  write(6,*) 'my_string = ',TRIM(my_string)

  end

  SUBROUTINE TESTER2(charac)
  implicit none
  character(LEN=100), allocatable :: charac(:)
C stuff for f2py. 
Cf2py character(LEN=100), allocatable, intent(in, out) :: charac(:)
  ALLOCATE (charac(4))
  charac(4) = 'I was added!'

  end

The following is my python call.

import test
text = "my_string"
a = b = c = d = 0
print(text, a, b, c, d)
text, a, b, c, d = test.tester(text, a, b, c, d)
print(a, b, c, d)
print("text = ", text)

text2 = ["first", "second", "third"]
print(text2)
text2 = test.tester2(text2)
print(text2)

I have run into two problems.

1) When I pass the string text to test.tester, my expected output is my_string. I was added!. However, I get b'my_string. I was added!. Where is the b' coming from and how do I get rid of it?

2) test.tester2 won't compile with f2py. I keep getting the error Tpye mismatch in argument 'charac': passed CHARACTER(1) to REAL(4). What have I done to cause this and how do I correct it?

Note: This is related to another question of mine Why does f2py not include all arguments?. I've been told what I'm trying to do here may correct that problem.

Community
  • 1
  • 1
jknicely
  • 317
  • 2
  • 3
  • 10
  • What is `b`, a blank? It is always put in the first column when you use `write (*,*)` – Vladimir F Героям слава Jul 01 '15 at 16:13
  • I know nothing of note about f2py/python, but you declare `my_string` in the subroutine as length 1000. Your python doesn't have an explicit length like this: will this cause problems? – francescalus Jul 01 '15 at 16:17
  • @ Valdimir F. `b` is the character b. When the fortran code writes `my_string`, `b'` is not there. When I print out `my_string` in python, the `b'` is at the start of the output after I run `test.tester`. @ francescalus. The fortran length shouldn't affect it, but I'll try setting the length as allocatable and play with that. – jknicely Jul 01 '15 at 16:23
  • f2py will not work with allocatable characters, it does not know these modern features. That is also the reason for your point 2). – Vladimir F Героям слава Jul 01 '15 at 16:28
  • 1
    The b' means that it is a byte string, have a look at http://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string-literal – Edmondo Giovannozzi Jul 02 '15 at 11:44
  • @ Vladimir F & Edmondo G. Thank you two for that information. I removed the allocatable bit and `tester2` sort of works now. It reads in the python list as a single string. I would rather not have to read in the entire list as a string and then have to parse it in my fortran code. Is there a way to pass the list to fortran as a character array? – jknicely Jul 08 '15 at 22:17

0 Answers0