-1

I am trying to read in a file 'BASTIFeH.txt' with astropy.ascii, that has more files embedded in it. Those embedded files get called my_ISOFILE1 or my_ISOFILE2. There seems to be no issue with this, it's when I start working with those that I seem to get issues. Here is the relevant snippet of my code:

isochrone = ascii.read(r'C:\Users\raeas\ThesisResearch\BastiFeH.txt')

for i in range(len(isochrone)):
    if BASTIFEH1 == isochrone['col2'][i]:
        my_ISOFILE1 = isochrone['col3'][i]
    if BASTIFEH2 == isochrone['col2'][i]:
        my_ISOFILE2 = isochrone['col3'][i]


TEMPTEFF = np.log10(Teff)


for j in range(len(my_ISOFILE1)):
        
    if my_ISOFILE1['col4'][j] < TEMPTEFF and (my_ISOFILE1['col1'][450] < my_ISOFILE1['col1'][j] < my_ISOFILE1['col1'][1189] or my_ISOFILE1['col1'][2065] < my_ISOFILE1['col1'][j] < my_ISOFILE1['col1'][2249]):
            
        LOGL1 = my_ISOFILE1['col3'][j]
        MV1 = my_ISOFILE1['col5'][j]
        MASS1 = my_ISOFILE1['col2'][j]
        
        NEWLOGG1 = 4.44 + np.log10(MASS1) - LOGL1 + 4*np.log10(Teff/5777)
                    
        break

The specific line that I am getting that error on is:

if my_ISOFILE1['col4'][j] < TEMPTEFF and (my_ISOFILE1['col1'][450] < my_ISOFILE1['col1'][j] < my_ISOFILE1['col1'][1189] or my_ISOFILE1['col1'][2065] < my_ISOFILE1['col1'][j] < my_ISOFILE1['col1'][2249]):

The thing that is confusing me so much is that that same syntax didn't bother it before this, only when I get here.

Thanks.

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
  • `my_ISOFILE` is returned from `isochrone['col3'][i]`. Are you sure IT is going to have a ['col4'] entry? Or did you mean `isochrone['col4'][j]`? – Tim Roberts Mar 08 '21 at 23:53
  • 'isochrone' only has three columns, but the files that are listed in column three of 'isochrone' (i.e. 'my_ISOFILE') have more than 4 columns – R.Stan27 Mar 09 '21 at 20:07
  • "the files that are listed in column three if 'isochrone'" I don't see you reading any additional files. – Tim Roberts Mar 09 '21 at 20:14
  • I mean that what column three of 'isochrone' is is a list of file names. – R.Stan27 Mar 10 '21 at 19:52
  • I get that. my_ISOFILE1 contains a file name. So when you refer to my_ISOFILE1['col4'], that fails because it's not a dictionary. It's a filename. You haven't read the contents of that file. – Tim Roberts Mar 10 '21 at 20:39

1 Answers1

0

It might help to know exactly what you're trying to do in order to point a better way, but here are some broad tips as well as an explanation of why it's not working.

First of all a generic tip: If you really want to write a loop over all rows in a table, you don't need to use range(len(table)) and integer indexing. It is usually more efficient to iterate over the rows directly which is what happens when you use the table itself as the iterable in the for loop syntax. What I mean is, you can write:

for row in isochrone:
    if bastifeh1 == row['col2']:
        my_isofile1 = row['col3']
    if bastifeh2 == row['col2']:
        my_isofile2 = row['col3']

(Note: While this is ultimately up to personal preference, it is more common to use all lower-case variable names in Python).

However, even if you rewrite the loop that way, it's not exactly clear what you want to do here, because even if the loop finds my_isofile1 and my_isofile2 it will continue to loop over the rest of the table. I'm not sure if that's what you want. The rest of your code doesn't even show what my_ISOFILE2 is used for though so I won't belabor that point for now.

As others have pointed out, if my_ISOFILE1 is supposed to be the path to a file, it is now a string, so in your next loop you are just looping over the characters in the string. You would need to open the table and loop over the actual data in the table (you can use for for row in table: like I showed above).

You can also speed things up and maybe make it a bit clearer by not repeatedly performing column look-ups,

col1 = my_table['col1']
col4 = my_table['col4']

for idx, value in enumerate(col4):
    if value < tempteff and (col1[450] < col1[idx] < col1[1189]...):
        # do something
        break

Additionally, while I'm sure there's a reason, it's a bit unusual to write a condition like this with very specific row indices. What is special about these rows, and is there not a computation that determined this which could be made more explicit?

Iguananaut
  • 21,810
  • 5
  • 50
  • 63