1

I am trying to parse some .out files to get a value E contained within each file, and then plot these value against theta and r as a 3d surface plot. The values of theta and r are contained in the .out file title names: H2O.r{}theta{}.out. I.e. r is given in the first {} and theta is then given in the next {}. r is given to 2 d.p and theta is given to 1 d.p. in the file names, e.g. r = 0.90, theta = 190.0.

I am having a hard time iterating through the files, and extracting this information into an array E . I have come across an error:

IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices.

However, if I change my r array to int to get rid of this error, then all of the values in r will become 0. Addtionally, my code to extract E from the file will no longer work as I will inputting 'H2O.r0.00theta70.out', a file which doesn't exist. Does anybody have any suggestions ?

from numpy import *
import matplotlib.pyplot as plt
import os
os.chdir('C:/Users/myName/ex2/all')

theta = arange(70.0, 161.0, 1, dtype = float)
r = arange(0.70, 1.95, 0.05, dtype = float)
r_2dp = [ '%.2f' % elem for elem in r_O ] # string array, with rounding to match the file names
E = zeros((theta.shape[0],r.shape[0]))

def extract(filename):    #extract value from file
        filename = open(filename,"r")
        for line in filename:
            if 'SCF Done' in line:
                l = line.split()
                p = float(l[4])
        return p

for i in r_2dp:    #create E array that will allow me to plot E vs r vs theta
    for j in theta:
        for filename in os.listdir('C:/Users/myName/ex2/all'):
            if filename.startswith('H2O'):
                filename = 'H2O.r{}theta{}.out'.format(i,j)                
                E[i,j] = extract(filename)
2
  • If I see this correctly, r_2dp is a string array; when you later loop through it and set E[i, j], i is a string which probably is the cause of the error you get. Commented Dec 16, 2017 at 0:11
  • Hi, I understand why the error comes up, I just don't know how I can avoid it. I need r_2dp to be a string array so that the filename = ... works. I don't know how to do this otherwise Commented Dec 16, 2017 at 0:13

1 Answer 1

1

One solution would be to associate an index with the filename which you can accomplish using enumerate; I think you can just change your loop to

for i, fn in enumerate(r_2dp):
    for j, ti in enumerate(theta):
        for filename in os.listdir('C:/Users/myName/ex2/all'):
            if filename.startswith('H2O'):
                filename = 'H2O.r{}theta{}.out'.format(fn,ti)                
                E[j,i] = extract(filename)

Please note that I changed E[i,j] to E[j,i] to get the dimensions correctly; you could also change the order of the two for-loops or initialize E the other way round...

Untested, as we cannot access your file, but the general idea should work...

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

3 Comments

Hi, this works if I enumerate for theta too, as this was a list of floats. But now I think there is a problem with the array size I am trying to fit these data values in to: IndexError: index 25 is out of bounds for axis 1 with size 25 . If I print E, some of the elements are populated with the right-looking values, but most are still 0
@loolipop: True, you also need enumerate for theta. To get rid of this error, either use E[j, i] or change the order of your loops for r_2dp and theta or initialze E the other way round. Again, untested.
Yep thankyou ! I also needed to remove the for filename in os.listdir... line as I was already in this directory, and this with the .format(fn,ti) meant that an infinite number of filename kept on being generated

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.