Skip to content

Instantly share code, notes, and snippets.

@kofemann
Created May 16, 2013 18:39
Show Gist options
  • Save kofemann/5593993 to your computer and use it in GitHub Desktop.
Save kofemann/5593993 to your computer and use it in GitHub Desktop.
import re
import string
import math
from Tkinter import Tk
from Tkinter import *
from tkFileDialog import askopenfilename, asksaveasfilename
import cStringIO
IN = 'COLLISON1.txt'
DELIMER = '\xb3'
DELIMER2 = '\x3f'
KeV_in_Mev=1000
data_line_re = re.compile(".+\d+\.E\+0.*")
dead_line_re = re.compile(".*(Target|Replacements).*")
def get_data_lines(raw_line):
return data_line_re.match(raw_line) and not dead_line_re.match(raw_line)
def dump_in_hex(s):
for c in s:
print "%02x" % ord(c)
def line_to_record(data_line):
data_line = data_line.replace(DELIMER2, DELIMER)
rf = string.split(data_line, DELIMER)
record = {}
record['ion'] = rf[1]
record['energy'] = float(rf[2])/KeV_in_Mev
record['depth'] = float(rf[3])
record['y'] = float(rf[4])
record['z'] = float(rf[5])
record['se'] = float(rf[6])
record['atom'] = string.strip(rf[7])
record['recoil_energy'] = float(rf[8])
record['target_disp'] = float(rf[9])
return record
def read_records(data_file):
f = open(data_file)
data_lines_only = filter(get_data_lines, f.readlines())
records = map(line_to_record, data_lines_only)
f.close()
return sorted (records, key = lambda record: record['depth'] )
if __name__ == '__main__':
Tk().withdraw()
filename = askopenfilename(title = "Select COLLISON file" )
outfile = asksaveasfilename(title = "Select output file", initialfile="result.txt" )
output = cStringIO.StringIO()
last_level = None
level_index = -1
levels = []
for r in read_records(filename):
atom = r['atom']
if atom != last_level:
level_index += 1
last_level = atom
levels.append({})
levels[level_index]['atom'] = atom
levels[level_index]['avg'] = 0.0
levels[level_index]['sigma'] = 0.0
levels[level_index]['count'] = 0.0
levels[level_index]['layer'] = level_index
levels[level_index]['depth0'] = 1 << 63
levels[level_index]['depth1'] = 0
run_info = levels[level_index]
current_avg = run_info['avg']
current_sigma = run_info['sigma']
current_count = run_info['count']
if run_info['depth0'] > r['depth']:
run_info['depth0'] = r['depth']
if run_info['depth1'] < r['depth']:
run_info['depth1'] = r['depth']
run_info['avg'] = (current_avg*current_count + r['energy']) / (current_count + 1)
run_info['sigma'] = (current_sigma*current_count + r['energy']*r['energy']) / (current_count + 1)
run_info['count'] = current_count + 1
output.write(" # Atom No. Col. E(avg) Sigma\n")
final_lavels = filter( lambda record: record['depth1'] - record['depth0'] != 0, levels)
final_layer = 0
for l in final_lavels:
final_layer += 1
l['layer'] = final_layer
sigma = math.sqrt(l['sigma'] - l['avg']*l['avg'])
out = "layer %2d %4s %12d %.2f %.2f\n" % (final_layer, l['atom'], l['count'], l['avg'], sigma)
output.write(out)
output.write('\n\n\n')
by_atom = {}
for l in final_lavels:
atom = l['atom']
if not by_atom.has_key(atom):
by_atom[atom] = []
by_atom[atom].append(l)
for atom_record in by_atom.keys():
vals = sorted(by_atom[atom_record], key = lambda record: record['layer'])
output.write("\nAtom: " + atom_record + '\n')
for v in vals:
sigma = math.sqrt(v['sigma'] - v['avg']*v['avg'])
out = "layer %2d %12d %.2f %.2f\n" % (v['layer'], v['count'], v['avg'], sigma)
output.write(out)
of = open(outfile, "w")
of.write(output.getvalue())
of.close()
root = Tk()
root.title("SRIM post-processing")
t = Text(root)
s = Scrollbar(root)
s.pack(side=RIGHT, fill=Y)
s.config(command=t.yview)
t.config(yscrollcommand=s.set)
t.insert(INSERT, output.getvalue())
t.focus_set()
t.pack()
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment