0

I'm converting some code from fortran90 to python 2.7 and am having trouble understanding the arguments the in Open, Write, and Read functions in the fortran code, as well as knowing what elements are necessary to add to the code when I write it in python.

Here are some blocks of the fortran code that I want to understand and convert to python:

OPEN(1,FILE=TRIM(filenameOut),RECL=2000)
WRITE(1,*) "tAge/yr  (u-r)  (u-z)  fGas  Mstars/MstarsOld"
CLOSE(1)

I'm guessing that the "1" is just assigning a label to the file name, the TRIM is removing any white space before or after the variable to which the python equivalent would be filenameOut.strip() or something. I'm unsure of what the RECL is doing and what the python equivalents for the other functions would be for this block.

Another example:

OPEN(1,FILE=TRIM(filenameOut),RECL=2000)
WRITE(1,*) "(u-r)  prob(u-r)"
DO countInside=1,nColourBins
    WRITE(1,*) uMinusrMidpointsArray(countInside),probuMinusrArray(countInside)
CLOSE(1)

From this I would guess that the asterisk is meaning to write to the next line in the file. Again, I'm not sure how to do this in python yet.

An example of reading in the fortran code:

OPEN(1,FILE=TRIM(filenameBC),RECL=2000)
  READ(1,*)
  READ(1,*)
  READ(1,*)
  READ(1,*)
  READ(1,*)
  READ(1,*)
  READ(1,*)

  IOEnd=0
  DO WHILE(IOEnd>-1)
     READ(1,*,IOSTAT=IOEnd) logTime,Mbol,g,uMg,gMr,gMi,gMz
  END DO
  CLOSE(1)

I understand that the repeated READ(1,*) are simply reading off the first 7 lines of the file, but I'm unsure of any python equivalent shortcuts for this, i.e. a way to start at the 8th line or something.

1
  • 2
    The control information list options for the input/output statements (they are statements, not functions) are very well documented in the standard, books, online tutorials, etc.. Instead of guessing at their meaning, it would be a far more constructive use of time to have a look at one of those. Commented Mar 31, 2013 at 13:44

3 Answers 3

2

as was noted last time you asked this, the recl= used with sequential access is not standard does nothing with at least one compiler, and almost certainly should be ignored.

In python you read lines as strings and process the string, something like this:

file=open(filename,'r')
for i in range(6):file.readline()   #skipping 6 lines
items=file.readline().split()

items holds your values as strings.. you then convert each based on type:

logTime=float(item[0])

i just winged this without testing..but it should get you started.

One gotcha, fortran with the "*" will read from multiple lines if needed. I doubt this is the case here but for completeness you need to do something like this,

items=[]
while len(items)<nrequired:items.extend(file.readline().split())

for writing you can try:

file=open(filename,'r')
file.write(' '.join([repr(x) for x in (v1,v2,v3)])+'\n')

or

file.write(('%.14g'+(' %.14g'*2)+'\n')%(v1,v2,v3)) 
                            (need to count how many values you have to put the "*2" )

neither of these gives you the exact output you'd get from fortran. If you need the same spacing, decimal places, etc see Vladimirs comment.. (If a value in python is 0.1 there is not a simple way to force printing the trailing zeros 0.100000 as you would get with fortran )

Sign up to request clarification or add additional context in comments.

4 Comments

That helps a lot thanks, I had forgotten the RECL was answered before, and yes I will ignore that.
Is the WRITE statement overwriting the file or appending the file? This will determine whether I want the argument 'w' or 'a' in python yes?
both fortran and python default to overwriting. I'll add a write example to my answer in a while..
note on my last edit i fixed two bugs.. split should have no argument and items=[] , not items=[,] ..
1

Your first example is clearly wrongly parenthesized. It should be like in the second one.

Trim only clears the trailing spaces. Here it is probably not needed.

In your case the file will be connected to the sequential access. In this case RECL sets the maximum record length for the file (this means line length). It is quite possible you can ignore it.

The asterisk in the second position of the I/O statements means the usage of the list-directed format. This gives the compiler some freedom in how exactly format the output. In particular, he compiler continues on the next line (record), if the output is too long, or if the input does not contain all te items in the current one.

So, READ(1,*,IOSTAT=IOEnd) logTime,Mbol,g,uMg,gMr,gMi,gMz can read from 1 line, if it contains 7 items, but it can also red from 7 lines with one item. You must count with this in your Python code, if you want to keep this flexibility.

WRITE(1,*) uMinusrMidpointsArray(countInside),probuMinusrArray(countInside) should write only to one line, as it is only two items, unless they are long strings.

3 Comments

The file does contain 7 items, so I guess it is just reading the one line. I'm not entirely sure what you mean about the asterisk giving the compiler some freedom. What would the python equivalent of these functions be given? Thanks!
They are not functions, they are statements. The freedom means that in output the processor can insert spaces and newlines as required. Also it can go to the next line when reading. Exactly like I wrote already.
Check this thread bytes.com/topic/python/answers/591102-reading-fortran-data and this package pypi.python.org/pypi/fortranformat . You can guess the correct python read routines from the file you did not show. We also do not and cannot know if you really need to keep the flexibility.
-1

from sys import argv

script,

filename = argv

txt = open(filename)

print "Here's your file %r:" % filename
print txt.read()

print "Type the filename again:"
file_again = raw_input("> ")

txt_again = open(file_again)

print txt_again.read()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.