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

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Reading output and producing input (post-processing)


    Hello,
    I am very new to python, as such, please do not let my question insult your intelligence.

    Essentially, I run analysis and use a lot of load combinations in order to yield the governing load combinations. Below is a single line from an output table:

    PART FS HS TAB IDs area LOAD-ID LIM PHA UR N1 N2 N12 M1 M2 M12 V1 V2
    W-CANT27 108 7 MAX X11 10000 BAS85336 ULS 45 0.78 1310 -1959 -4640 1 2 0 -1 1

    What I currently do in order to incorporate these governing loads in my new input file is copy the unique BAS# into an input file in order to run that.

    You may wonder why I need to re-run this as I already have my results. The reason is that the supplementary visualizing software cannot produce plots based on thousands of combinations (well, it can but it would take days), but it can do so when I select, letís say, 50 governing combinations (one such combination is seen in the line above).

    As you may have figured out, what I hope to do is take a unique BAS combination and place it into an input file.
    The NEW Load Combination input file (that originally contains all the combinations) will still have all the combinations but as you can see in the following line is commented (%) out and then the loads needed are uncommented:

    % DECAS DWP=4 LS=ULS PHA=45,60,75 BAS=85336:A

    to yield

    DECAS DWP=4 LS=ULS PHA=45,60,75 BAS=85336:A

    Ultimately, I would like python to select the unique BAS# from the output file (first line) and uncomment this from the input file.

    A BAS combination can be governing for several regions of the structure. I have been able to extract all the BAS combinations, but my problem is using the list in which these combinations are stored in order to uncomment the line in which the combination exists. The following code is as basic as it gets, but the reason for my uselessly long explanation was in the hopes of getting some help here too:

    Code:
    # -*- coding: utf-8 -*-
    """
    Created on Wed Nov 06 14:52:34 2013
    
    @author: ceand
    """
    
    
    UR1=[]
    UR2=[] 
    UR3=[]
    UR4=[]
    UR5=[]
    BAS=[]
    BAS1=[]
     
    for line in open('W-CANT2-RADWALL27-DESIGN-ULS.out'):
        if 'BAS' in line:
            words=line.split()
            #if any('BAS71432' in i for i in words):
            for i, j in enumerate(words):
                if  i == 6:
                    UR1.append(j)
                    #print 'UR1 = ', UR1
                if i == 5:
                    UR2.append(j)
                    #print 'UR2 = ', UR2
    
    
    for k in UR1:
        if k.find('BAS') != -1:
            UR3.append(k)
    
    for j in UR2:
        if j.find('BAS') != -1:
            UR4.append(k)
    
    for f in UR3:
        UR5.append(f)
        
    for l in UR4:
        UR5.append(l)
    
    for x in UR5:
        if x not in BAS:
          BAS.append(x)
    BAS.sort()
    
    for u in BAS:
        select= u[3:8]
        BAS1.append(select)
    print BAS
    print BAS1
    
    with open("DECAS-ULS-W-RAD2-AREA-530.INP", 'r+') as fout:
        for line in fout:
            if any(s in line for s in BAS1):
                line = line.translate(None, '%')
    fout.close()
    Code output:
    Code:
    ['BAS25001', 'BAS25202', 'BAS25203', 'BAS31715', 'BAS41732', 'BAS71432', 'BAS71433', 'BAS71435', 'BAS71766', 'BAS72766', 'BAS73766', 'BAS85326', 'BAS85336', 'BAS85346', 'BAS86014', 'BAS86315', 'BAS86333', 'BAS86345', 'BAS86355']
    ['25001', '25202', '25203', '31715', '41732', '71432', '71433', '71435', '71766', '72766', '73766', '85326', '85336', '85346', '86014', '86315', '86333', '86345', '86355']
    Thanks for any guidance.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    What I've determined from your description is that your program copies data from one file to another but filtering out '%' characters that start lines containing 'BAS'. ?????
    [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
    Nov 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by b49P23TIvg
    What I've determined from your description is that your program copies data from one file to another but filtering out '%' characters that start lines containing 'BAS'. ?????
    Well, you just made me look silly for writing so much, but essentially yes.

    Edit** lines contain a unique BAS#, the number occurs in one line and it is in the beginning of this line '%' needs to be removed.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    And what constitutes "unique"? Consider entire file? set of all of the BAS numbers found?

    Something like this but I don't remember if the re function is search or find and I'd need to read the documentation about groups and extracting a group from a match object to get the number.
    Code:
    import re
    BAS = re.compile('^%.*BAS[0-9]')
    
    with open('W-CANT2-RADWALL27-DESIGN-ULS.out') as inf:
        BASlines = [LINE[1:] for LINE in inf if BAS.search(LINE)]
    
    # BASlines is a list of the matching lines with '%' character removed.
    If you're in the area, please join us for the NYC J user group meeting in the Heartland Brewery, Empire State building. 6:30 PM to 8:30. Meeting starts with "beginner's regatta".
    [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
    Nov 2013
    Posts
    4
    Rep Power
    0
    Hi,

    I read a bit about regular expressions which seem handy (thanks for info). I will try to incorporate it.

    Unique in the sense that the new input file DECAS-ULS-W-RAD2-AREA-530.INP (that needs the % removed) has one line per individual BAS numbers (e.g. 25001, 25002, etc..)

    Consequently, from the results of my .out file I will need to uncomment 19 lines in my .inp file as there are 19 BAS#s in the list produced (see code output). Essentially, the numbers themselves are "unique" to this analysis/file. It is the number itself I want to find in the seperate lines and then remove the % of that line.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    Sorry, I don't feel like playing with all the nuances such as "can BAS be the first word on a line" and "can BAS appear several times on a line" and "what if BAS is part of a longer word?" These are all treatable with re.

    I use emacs regular expressions many times daily. The syntax is is a little different from that of the re module and I don't feel like looking up "is the substitutution method repl, replace, sub, subst, subn, or substitute" and "what is the symbol to match `start of word'". And there's the re iterator as well.

    for match_object in regular_expression_iterator(sentence):
    #use the match_object

    The 95% solution (if you can handle 95% of the cases with something simple then do it) works quite well for me.

    The idea though is to make a regular expression group that matches digits following BAS. Then use substitution to extract the digits.

    The pattern is something like this (but maybe there are backslashes)

    pattern = 'BAS([0-9]+)'

    And the substitution to replace the sentence with the text of the first match is something like

    pattern.subs(r'\1', sentence)

    And you might have to contend with unicode, str, bytes objects as well depending on data source and python version.

    Good luck.

    [edit]With emacs all it takes is a few keystrokes. I immediately see whether the alteration works. If it's wrong the deep undo feature restores prior state quickly. Repeated application is a good reason to prefer python for your job.[/edit]
    Last edited by b49P23TIvg; November 13th, 2013 at 11:48 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0
    I appreciate the help. I will check out your suggestion in more detail.

    For the sake of explanation the output files are all in one format. Two tables are produced leaving the BAS in either line pos. 5 or 6 i.e. the reason for my code checking these locations for BAS and then filtering out anything in the list that isnt BAS. My list of BAS is therefore complete and in my new input file in which every line is identical (one shown above) I need to find one line per one BAS and have the % removed.

    Nontheless, I will look into what you told me and figure it out. Thank you.

IMN logo majestic logo threadwatch logo seochat tools logo