Trying to convert a text file to python from fortran

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 759 Views - Last Post: 09 January 2012 - 07:36 AM Rate Topic: -----

Topic Sponsor:

#16 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 443
  • View blog
  • Posts: 792
  • Joined: 08-June 10

Re: Trying to convert a text file to python from fortran

Posted 06 January 2012 - 09:35 AM

You've put the initialization inside the for loop again, which is wrong. Look at my code and note that smallest and largest are before their respective loops.
Was This Post Helpful? 1
  • +
  • -

#17 krajand14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-October 11

Re: Trying to convert a text file to python from fortran

Posted 06 January 2012 - 02:39 PM

Ok so i copied and pasted your code and it made no change, is there anything else you can thing of because I am at a loss here.
Was This Post Helpful? 0
  • +
  • -

#18 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 443
  • View blog
  • Posts: 792
  • Joined: 08-June 10

Re: Trying to convert a text file to python from fortran

Posted 06 January 2012 - 02:41 PM

Without your data set I'm flying blind. Please post the file you are trying to parse along with your latest version of the code.
Was This Post Helpful? 0
  • +
  • -

#19 krajand14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-October 11

Re: Trying to convert a text file to python from fortran

Posted 06 January 2012 - 02:51 PM

I attached the text file, and here is the most updated code:
import math
def get_data(f_obj,data_dict):
    ret = []
    for line in f_obj:
        ret.append(line.split())
        for val in ret:
            for i in range(0,len(ret)):
                data_dict[ret[i][0]]=ret[0][1:]

def compare_lrg(data_dict,index):
    largest = float('-inf')
    for key in data_dict.keys():
        rxn_lrg = int(key)
        if data_dict[key][index] == 'NaN':
            continue
        elif data_dict[key][index] > largest:
            largest = float(data_dict[key][index])
            rxn_lrg = int(key)
    print largest, 'rxn:',rxn_lrg
 
def compare_sml(data_dict,index):
    smallest = float('inf')
    for key in data_dict.keys():
        rxn_small = int(key)
        if data_dict[key][index] == 'NaN':
            continue
        elif data_dict[key][index] < smallest:
            smallest = float(data_dict[key][index])
            rxn_small = int(key)
    print smallest, 'rxn:',rxn_small
    
def main(file_str= 'reaction_results.txt'):
    file_obj = open(file_str, 'r')
    data_dict = {}
    get_data(file_obj,data_dict)
    file_obj.close()
    print '*'*20
    print 'Linear Interpolation data'
    print '*'*20
    print
    print '*'*20
    print 'Largest rms:'
    compare_lrg(data_dict,1)
    print '*'*20
    print 'Smallest rms:'
    compare_sml(data_dict,1)
    print '*'*20
    print 'Max error:'
    compare_lrg(data_dict,2)
    print '*'*20
    print 'Largest T_max:'
    compare_lrg(data_dict,3)
    print '*'*20
    print 'Largest R_max:'
    compare_lrg(data_dict,4)
    print '*'*20
    print 'Largest Local max:'
    compare_lrg(data_dict,5)
    print '*'*20
    print
    print '*'*20
    print 'Logarithmic Interpolation data'
    print '*'*20
    print
    print '*'*20
    print 'Largest rms:'
    compare_lrg(data_dict,6)
    print '*'*20
    print 'Smallest rms:'
    compare_sml(data_dict,6)
    print '*'*20
    print 'Max error:'
    compare_lrg(data_dict,7)
    print '*'*20
    print 'Largest T_max:'
    compare_lrg(data_dict,8)
    print '*'*20
    print 'Largest R_max:'
    compare_lrg(data_dict,9)
    print '*'*20
    print 'Largest Local max:'
    compare_lrg(data_dict,10)
    print '*'*20
    
    return data_dict

main()

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#20 wordswords  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 163
  • Joined: 17-December 11

Re: Trying to convert a text file to python from fortran

Posted 06 January 2012 - 05:36 PM

I have upvoted most of Motoma's posts here because it seems he is tirelessly struggling to fix someone else's newbie code, and deserves some kudos.
Was This Post Helpful? 0
  • +
  • -

#21 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 443
  • View blog
  • Posts: 792
  • Joined: 08-June 10

Re: Trying to convert a text file to python from fortran

Posted 09 January 2012 - 07:36 AM

You had a problem with your get_data function, essentially, line 8 puts the first line's data in every element. Here is a proper function:

def get_data(f_obj,data_dict):
    for line in f_obj:
        dat = line.split()
        data_dict[dat[0]] = dat[1:]



With that fixed, you now have to deal with all of the "-312"s in your data file.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2