Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #16  
Old January 8th, 2013, 08:07 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 10 m 17 sec
Reputation Power: 383
Something like this

See attached .pdf . We handled the graph back in post 8, and the curve fitting in post 12.

If I weren't a nuclear engineer I wouldn't help so much.
Code:
def reports(output):
    with open(output) as f:
        count = 1                  #this defines the number of tallies in the output file
        x = 101                    #this is the first surface number
        r = str('surface  %d' % x) #r is a variable used to help find the point where the results start.
        d = 0 #initializes depth variable
        c = 0 #intilizes the c variable used in the while loop
        p = 0 #initializes variable for calculating percent change
        #The following while loop skips each line until it comes across the line defined in r
        Flux = []
        SEM = []
        Depth = []
        Change = []
        while count < 10:
               while c != r:
                   line = f.readline()
                   c = line.strip()
               line = f.readline() #reads the energy and SEM
               numbers = line.strip().split()
               Flux.append(numbers[0])
               SEM.append(numbers[1])
               Depth.append(d)
               p = '%g'%(100*((float(Flux[0])-float(Flux[-1])) / float(Flux[0]))) #calculates percent change
               Change.append(p)
               d += 4
               count += 1
               x += 4
               r = str('surface  %d' %x)
        with open('Report.tex', 'w') as table:
            table.write(START)
            for T in zip(Depth,Flux,Change,SEM,):
                table.write('%d & %9.3e & %5.1f & %6.4f\\\\\n'%tuple(float(x) for x in T))
            table.write('\\hline\n')
            table.write(END)

        print('Report.tex created, now command   pdflatex Report  .')


START = r'''
\documentclass[12pt]{report} %

\begin{document} %

MCNPX Simulation For XXXXX %

\begin{tabular}{|lrrr|} %
\hline %
Depth&\multicolumn{1}{c}{Flux}&\multicolumn{1}{c}{Reduction}&\multicolumn{1}{c|}{SEM}\\ %
&\multicolumn{1}{c}{$\frac{n}{cm^2s}$}&\multicolumn{1}{c}{\%}&\multicolumn{1}{c|}{\ }\\ \hline %
'''

END = r'''
\end{tabular}
\end{document}
'''
Attached Files
File Type: pdf Report.pdf (30.3 KB, 18 views)
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : January 8th, 2013 at 08:11 PM.

Reply With Quote
  #17  
Old January 8th, 2013, 10:15 PM
danipoak danipoak is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 23 danipoak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 20 m 58 sec
Reputation Power: 0
Wow...I guess you know your stuff. It all makes sense now though! So I guess you recognized the MCNP output file right away then? I could tell you were a scientist/engineer of some sort just because of how familiar you were with processing data. But nuclear engineer as well, no too many of us out there.

So now we are geting into LaTex with creating a pdf report. I don't have LaTex installed right now. The mac installer file is quite large (>2 gigs) so I am going to wait until I am at school tomorrow to download at install it. Since I am not familiar with LaTex in order to create the pdf report I will have to exit out of the python command line and run pdflatex separately correct?

I am looking into gnuplot. It looks like the graphs I typically see in the literature I read so it is nice to finally know where they come from. It looks like it has a small learning curve but not much, really just command line stuff. From my limited research it is probably best to have the data output into a text document like I had earlier for gnuplot right?

Reply With Quote
  #18  
Old January 9th, 2013, 05:19 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 10 m 17 sec
Reputation Power: 383
I recognized neutron flux.

Python has a subprocess module; you could run pdflatex from python. For standard reports this might even be a good plan. Form letters to a zillion people with only name address changes, for example. I customize my reports in a text editor (emacs) and run latex by hand.

This online book is pretty good.

Reply With Quote
  #19  
Old January 9th, 2013, 12:57 PM
danipoak danipoak is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 23 danipoak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 20 m 58 sec
Reputation Power: 0
Here is the output from the a full blown output file. Looks really nice!

https://dl [dot] dropbox [dot] com/u/14334980/Report.pdf

I think something with the mac command line doesn't allow me to execute latex while in python but it is easy to just ctrl+d out and then type the command.

Reply With Quote
  #20  
Old January 9th, 2013, 01:08 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 10 m 17 sec
Reputation Power: 383
awesome!

The row starting with 148 is messed up.

You might be able to suspend python with C-z
(^Z), run latex and xdvi, then resume python.

Commands like

fg
jobs
bg

are built into the shell. `man bg' won't help, you need
man bash


You can also open many terminal windows.

I use about 4 shell buffers in emacs.

Reply With Quote
  #21  
Old January 10th, 2013, 05:04 PM
danipoak danipoak is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 23 danipoak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 20 m 58 sec
Reputation Power: 0
I'm doing a slight modification to this program now to read a different kind of file. I am currently getting this error:

Code:
>>> from rnaa import reports
>>> reports('naa')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "rnaa.py", line 22, in reports
    energy.append(numbers[0])
IndexError: list index out of range
>>> 


This happens when my second while loop is as follows with the variable b being anything greater than 1:
Code:
 while b<4097:
            line = f.readline() #reads the energy and SEM
            numbers = line.strip().split()
            energy.append(numbers[0])
            counts.append(numbers[1])
            SEM.append(numbers[2])
            b+=1


If I set b to less than 1 the file runs but of course my graph and report come up as only the first line of the data I want. The entire program is as follows:

Code:
def reports(output):
    with open(output) as f:
        r = str('cell  400') #r is a variable used to help find the point where the results start.
        c = 0 #intilizes the c variable used in the while loop
        #The following while loop skips each line until it comes across the line defined in r
        import numpy as np
        import matplotlib.pyplot as plt
       
        while c != r:
            line = f.readline()
            c = line.strip()
        
        line = f.readline()
        line = f.readline()
        b=0
        energy = []
        counts = []
        SEM = []
        while b<4097:
            line = f.readline() #reads the energy and SEM
            numbers = line.strip().split()
            energy.append(numbers[0])
            counts.append(numbers[1])
            SEM.append(numbers[2])
            b+=1
           
        table = open('Report.txt', 'w')
        table.write('================================================================ \n')
        table.write('MCNPX NAA Simulation For XXXXX                                   \n')
        table.write('================================================================ \n')
        table.write('================================================================ \n')
        table.write('Energy(MeV)          Counts          SEM             \n')
        table.write('================================================================ \n')
        for (E,C,S,) in zip(energy,counts,SEM,):    ####### zip is roughly a matrix transposition.
            table.write(str(E).ljust(8) + '     ' +
                        str(C).ljust(10) + '     ' +
                        str(S) + '\n')
        print('Report created')
        table.close()
        
        
        
        plt.rcParams['legend.loc'] = 'best'
        plt.figure(1)
        plt.xlim(1e-6, 3)
        plt.autoscale(enable=True, axis='y', tight=None)
        plt.plot(energy, counts, 'b')
        plt.xlabel('Energy (MeV)')
        plt.ylabel('Counts')
        plt.title('Gamma Spectrum')
        
        print('plots created')
        plt.show()


The input file I am using is located here:
https://dl [dot] dropbox [dot] com/u/14334980/naa

So I understand the problem based on the output is that the program is not going onto and reading the next line but I do not understand why it is not.

Reply With Quote
  #22  
Old January 10th, 2013, 09:02 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 10 m 17 sec
Reputation Power: 383
I deleted the plt lines, which don't seem like they'd interfere, and was unable to duplicate the problem.

Something weird happens at
5.11E-01 7.25E-08 0.0027

Reply With Quote
  #23  
Old January 10th, 2013, 10:08 PM
danipoak danipoak is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 23 danipoak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 20 m 58 sec
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
I deleted the plt lines, which don't seem like they'd interfere, and was unable to duplicate the problem.

Something weird happens at
5.11E-01 7.25E-08 0.0027


Well this is very weird then. I to deleted them to see if I could duplicate your results but I still get the index out of range error. What happens for you at that 0.0027 line? That's roughly 25-30% through the data which is odd. I'm going to try running this on some other computers at school tomorrow to see if I can duplicate the results there.

Reply With Quote
  #24  
Old January 11th, 2013, 02:53 PM
danipoak danipoak is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 23 danipoak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 20 m 58 sec
Reputation Power: 0
Well I figured it out. The program worked just fine. Something about how OSX makes .txt files is messed up. I shortened the original MCNPX output file just for testing purposes and something happened in that process. After being frustrated with the program all morning I finally ran the original output file and everything worked like it should. Stupid mac. Thanks again for your continued help. Here is the final output for this program:

https://dl [dot] dropbox [dot] com/u/14334980/naa.png

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Data processing script

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap