#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    I am new and need help with extracting certain data from a text file


    Assume the following is the data in ctf_output.txt, I would like to extract title headings from Rod #, Surface Temp and Centerline Temp, and then only the highest temperature for each and for each rod. Notice I put specific and significantly higher temps in each column with no repeats.

    Rod 1
    Surface Temperature Centerline Temperature
    500 510
    501 511
    502 512
    503 513
    504 525
    505 515
    535 516
    507 517
    508 518
    509 519
    510 520
    Rod 2
    Surface Temperature Centerline Temperature
    500 510
    501 511
    502 512
    503 513
    504 555
    505 515
    540 516
    507 517
    508 518
    509 519
    510 520
    Rod 3
    Surface Temperature Centerline Temperature
    500 510
    501 511
    502 512
    503 513
    567 514
    505 515
    506 559
    507 517
    508 518
    509 519
    510 520

    How can I do this with python? I need a python script that will pull the data and populate a new output file with a format of:
    Rod 1
    Surface Temperature Centerline Temperature
    535 525

    Rod 2
    Surface Temperature Centerline Temperature
    540 555

    Rod 3
    Surface Temperature Centerline Temperature
    567 559
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,961
    Rep Power
    481
    Code:
    # with more than 2 data columns I would write the program differently.
    
    with open('c' or 'ctf_output.txt') as inf:
        lines=inf.readlines()
    
    s = 0
    junk = [-666e99]*2
    
    for line in lines:
        if 'Rod' in line:
            if s:
                print('{}\t {}'.format(max(s), max(c)))
            print(line.strip())
        elif 'Surface' in line:
            print(line.strip())
            (s, c,) = ([], [],)
        else:
            try:
                field_values = list(map(int,line.split()))
            except ValueError:
                field_values = junk
            s.append(field_values[0])
            c.append(field_values[1])
    
    if s:
        print('{}\t {}'.format(max(s), max(c)))
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0
    How would you do it if there were multiple columns and the columns that I gave were in the middle of the others?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,961
    Rep Power
    481
    I wouldn't attempt this stunt without seeing the input.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0
    This is what an actual excerpt of the file is. Keep in mind that this is for one rod. I would like to extract the highest temperature for all rods in the file. Also the formatting is restricted by what I can put in the window.


    **************************************************************************************************** ******************************


    heater rod number 45 simulation time = 6.00 seconds
    surface no. 4 of 4
    -------------------- conducts heat to channels 60 0 0 0 0 0 geometry type = 1
    and azimuthally to surfaces 3 and 1 no. of radial nodes = 2

    **************************************************************************************************** *******************************

    rod axial fluid temperatures surface heat heater rod temperatues,
    node location (deg-f) heat flux transfer (deg-f)
    no. (in.) liquid vapor (b/h-ft2) mode surface center
    ---- -------- ------ ----- --------- -------- ------- ------

    74 144.00 540.9 546.8 0.8997E+04 subc 546.19 553.32 0.0000E+00
    73 * 143.00 542.8 546.8 0.3791E+05 subc 553.10 580.12 0.0000E+00
    72 * 141.00 543.4 546.8 0.8343E+05 subc 558.89 617.11 0.0000E+00
    71 * 139.00 543.4 546.8 0.9768E+05 subc 560.33 628.25 0.0000E+00
    70 * 137.00 543.4 546.8 0.1055E+06 subc 561.08 634.28 0.0000E+00
    69 * 135.00 543.4 546.8 0.1132E+06 subc 561.81 640.21 0.0000E+00
    68 * 133.00 543.4 546.8 0.1204E+06 subc 562.47 645.75 0.0000E+00
    67 * 131.00 543.4 546.8 0.1275E+06 subc 563.11 651.19 0.0000E+00
    66 * 129.00 543.5 546.8 0.1346E+06 subc 563.73 656.56 0.0000E+00
    65 * 127.00 543.5 546.8 0.1411E+06 subc 564.30 661.54 0.0000E+00
    64 * 125.00 543.5 546.8 0.1476E+06 subc 564.88 666.47 0.0000E+00
    63 * 123.00 543.5 546.9 0.1540E+06 subc 565.55 671.42 0.0000E+00
    62 * 121.00 543.5 546.9 0.1599E+06 subc 566.18 675.98 0.0000E+00
    61 * 119.00 543.5 546.9 0.1657E+06 subc 566.72 680.40 0.0000E+00
    60 * 117.00 543.6 546.9 0.1718E+06 subc 567.24 684.92 0.0000E+00
    59 * 115.00 543.6 546.9 0.1787E+06 subc 567.85 690.15 0.0000E+00
    58 * 113.00 543.7 546.9 0.1859E+06 subc 568.46 695.49 0.0000E+00
    57 * 111.00 543.7 546.9 0.1929E+06 subc 569.06 700.76 0.0000E+00
    56 * 109.00 543.8 547.0 0.1995E+06 subc 569.64 705.67 0.0000E+00
    55 * 107.00 543.8 547.0 0.2060E+06 subc 570.23 710.52 0.0000E+00
    54 * 105.00 543.9 547.0 0.2128E+06 subc 570.87 715.59 0.0000E+00
    53 * 103.00 543.9 547.0 0.2210E+06 subc 571.83 721.85 0.0000E+00
    52 * 101.00 544.0 547.0 0.2294E+06 subc 572.77 728.25 0.0000E+00
    51 * 99.00 544.0 547.0 0.2368E+06 subc 573.49 733.74 0.0000E+00
    50 * 97.00 544.1 547.1 0.2378E+06 subc 573.75 734.65 0.0000E+00
    49 * 95.00 544.1 547.1 0.2378E+06 subc 573.95 734.79 0.0000E+00
    48 * 93.00 544.1 547.1 0.2387E+06 subc 574.22 735.63 0.0000E+00
    47 * 91.00 544.2 547.1 0.2455E+06 subc 574.92 740.69 0.0000E+00
    46 * 89.00 544.2 547.1 0.2532E+06 subc 575.68 746.44 0.0000E+00
    45 * 87.00 544.2 547.1 0.2599E+06 subc 576.37 751.46 0.0000E+00
    44 * 85.00 544.2 547.1 0.2608E+06 subc 576.63 752.26 0.0000E+00
    43 * 83.00 544.2 547.1 0.2606E+06 subc 576.78 752.31 0.0000E+00
    42 * 81.00 544.2 547.2 0.2610E+06 subc 576.90 752.69 0.0000E+00
    41 * 79.00 544.1 547.2 0.2644E+06 subc 577.15 755.12 0.0000E+00
    40 * 77.00 544.1 547.2 0.2683E+06 subc 577.43 757.91 0.0000E+00
    39 * 75.00 544.0 547.2 0.2717E+06 subc 577.69 760.35 0.0000E+00
    38 * 73.00 544.0 547.2 0.2722E+06 subc 577.72 760.69 0.0000E+00
    37 * 71.00 543.9 547.2 0.2722E+06 subc 577.71 760.67 0.0000E+00
    36 * 69.00 543.8 547.2 0.2717E+06 subc 577.65 760.30 0.0000E+00
    35 * 67.00 543.7 547.3 0.2683E+06 subc 577.38 757.82 0.0000E+00
    34 * 65.00 543.6 547.3 0.2644E+06 subc 577.05 754.98 0.0000E+00
    33 * 63.00 543.4 547.3 0.2610E+06 subc 576.74 752.46 0.0000E+00
    32 * 61.00 543.3 547.3 0.2605E+06 subc 576.61 752.02 0.0000E+00
    31 * 59.00 543.1 547.3 0.2605E+06 subc 576.49 751.91 0.0000E+00
    30 * 57.00 542.9 547.3 0.2595E+06 subc 576.35 751.14 0.0000E+00
    29 * 55.00 542.7 547.3 0.2527E+06 subc 575.76 746.14 0.0000E+00
    28 * 53.00 542.4 547.4 0.2449E+06 subc 575.08 740.41 0.0000E+00
    27 * 51.00 542.1 547.4 0.2380E+06 subc 574.45 735.35 0.0000E+00
    26 * 49.00 541.8 547.4 0.2371E+06 subc 574.26 734.54 0.0000E+00
    25 * 47.00 541.4 547.4 0.2371E+06 subc 574.14 734.43 0.0000E+00
    24 * 45.00 541.0 547.4 0.2360E+06 subc 573.93 733.54 0.0000E+00
    23 * 43.00 540.5 547.4 0.2286E+06 subc 573.17 727.99 0.0000E+00
    22 * 41.00 540.1 547.4 0.2202E+06 subc 572.29 721.63 0.0000E+00
    21 * 39.00 539.6 547.4 0.2120E+06 subc 571.38 715.39 0.0000E+00
    20 * 37.00 539.1 547.5 0.2053E+06 subc 570.63 710.25 0.0000E+00
    19 * 35.00 538.6 547.5 0.1988E+06 subc 569.89 705.27 0.0000E+00
    18 * 33.00 537.8 547.5 0.1923E+06 subc 569.12 700.19 0.0000E+00
    17 * 31.00 536.9 547.5 0.1852E+06 subc 568.27 694.71 0.0000E+00
    16 * 29.00 535.8 547.5 0.1781E+06 subc 567.37 689.12 0.0000E+00
    15 * 27.00 534.5 547.5 0.1711E+06 subc 566.44 683.59 0.0000E+00
    14 * 25.00 533.0 547.5 0.1651E+06 subc 565.56 678.75 0.0000E+00
    13 * 23.00 531.2 547.5 0.1593E+06 subc 564.64 673.98 0.0000E+00
    12 * 21.00 529.0 547.5 0.1534E+06 subc 563.74 669.17 0.0000E+00
    11 * 19.00 527.4 547.6 0.1470E+06 subc 562.73 663.94 0.0000E+00
    10 * 17.00 526.4 547.6 0.1406E+06 subc 561.59 658.51 0.0000E+00
    9 * 15.00 525.4 547.6 0.1340E+06 subc 560.37 652.96 0.0000E+00
    8 * 13.00 524.5 547.6 0.1270E+06 subc 559.03 646.95 0.0000E+00
    7 * 11.00 523.6 547.6 0.1200E+06 subc 557.58 640.80 0.0000E+00
    6 * 9.00 522.8 547.6 0.1128E+06 subc 556.03 634.47 0.0000E+00
    5 * 7.00 522.0 547.6 0.1052E+06 subc 554.24 627.57 0.0000E+00
    4 * 5.00 521.3 547.6 0.9742E+05 subc 552.26 620.42 0.0000E+00
    3 * 3.00 520.6 547.6 0.8330E+05 subc 548.39 607.08 0.0000E+00
    2 * 1.00 520.0 547.7 0.3911E+05 spl 533.15 562.09 0.0000E+00
    1 0.00 520.0 547.7 0.1038E+05 spl 523.51 532.69 0.0000E+00
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0
    Here is my email if you'd like, I will send you the file.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0
    dustin.t.smith81atgmailcom
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,961
    Rep Power
    481
    Code:
    '''
        sample run, based on privately transfered data file
    
        $ python p.py
        Maximum temperature, dF
        Center	Surface
        696.18	574.07	{'rod': 6, 'surface': 2, 'time': 2}
        697.80	582.81	{'rod': 12, 'surface': 2, 'time': 2}
        669.13	576.97	{'rod': 18, 'surface': 2, 'time': 2}
        756.72	576.97	{'rod': 6, 'surface': 2, 'time': 6}
        761.55	587.97	{'rod': 12, 'surface': 2, 'time': 6}
        719.42	581.43	{'rod': 18, 'surface': 2, 'time': 6}
    '''
    
    # floating point comparison tolerance
    # used to match '6' with 'simulation time =      5.99907  seconds'
    TOLERANCE = 0.1
    
    import re
    
    class RodResults:
    
        def __init__(self, filename='deck.out'):
            with open(filename) as inf:
                self.lines = inf.readlines()
            self.markers = {}
            i = self.advance(0, 'total no. of axial nodes')
            self.markers['axial nodes'] = int(self[i].split()[-1])
            i = self.advance(i, 'no. of fuel rods')
            self.markers['fuel rods'] = int(self[i].split()[5])
            L = []
            self.markers['channel results'] = L
            while True:
                try:
                    i = self.advance(i, 'channel results')
                except:
                    break
                L.append(i)
                i += 1
    
        def advance(self, i, pattern):
            '''
                candidate for python3 next where next receives information
            '''
            while pattern not in self[i]:
                i += 1
            return i
    
        def advance_pattern_function(self, i, search):
            while not search(self[i]):
                i += 1
            return i
    
        def __len__(self):
            return len(self.lines)
    
        def __getitem__(self, item):
            return self.lines[item]
    
        def line_index_of_time_rod_surface(self, time, rod, surface,):
            '''
                extract lines from file including header
            '''
            for i in self.markers['channel results']: # find the correct time
                i = self.advance(i, 'heater rod number')
                if ('simulation time' in self[i]) and (abs(float(self[i].split()[-2])-time) < TOLERANCE):
                    break
            nodes = self.markers['axial nodes']
            while True:                     # find correct rod
                i = self.advance(i, 'simulation time')
                if int(self[i].split()[3]) == int(rod):
                    break
                i += nodes
            while True:                     # find surface
                i = self.advance(i, 'surface no.')
                fields = self[i].split()
                if int(fields[2]) == int(surface):
                    break
                if fields[2] == fields[4]:
                    raise IndexError('surface not found')
                i += nodes
            start = i-1
            assert 'heater rod number' in self[start]
            finish = self.advance(i+nodes, '*'*40)
            return (start, finish,)
    
        def get_rod_surface_nodal_results(self, time, rod, surface,):
            (start, finish,) = self.line_index_of_time_rod_surface(time, rod, surface,)
            i = self.advance_pattern_function(start, re.compile('^ *[1-9][0-9]* ').search)
            j = self.advance_pattern_function(i, lambda s: not re.compile('^ *[1-9][0-9]* ').search(s))
            assert j <= finish
            return self[i:j]
    
    
    def extract_column(lines, column_number, separator = None):
        return [line[column_number:].split(separator)[0] for line in lines]
    
    simulation_result = RodResults('deck.out')
    
    
    
    print('Maximum temperature, dF')
    print('Center\tSurface')
    for time in (2, 6,):
        for rod in (6, 12, 18,):
            request = dict(time = time,rod = rod,surface = 2)
            lines = simulation_result.get_rod_surface_nodal_results(**request)
            center_dF = extract_column(lines, column_number = 81) # dF.  good grief.
            surface_dF = extract_column(lines, column_number = 70) # btus, hours, feet.  good grief.
            print('{}\t{}\t{}'.format(max(center_dF,key=float),max(surface_dF,key=float),request))
    Last edited by b49P23TIvg; April 11th, 2013 at 12:13 AM. Reason: except after c
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo