Skip to content

Instantly share code, notes, and snippets.

@tkanmae
Last active December 26, 2015 15:39
Show Gist options
  • Save tkanmae/7174475 to your computer and use it in GitHub Desktop.
Save tkanmae/7174475 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import functools
from lib import lxcat
txt_file = '20131029_Electron_Scattering_Swarm_Data_Dutton_1975.txt'
recs = lxcat.load_swarm(txt_file)
def print_record(rec, fh):
p = functools.partial(print, file=fh)
for k, v in rec.items():
if k != 'DATA':
p('{0}: {1}'.format(k, v))
else:
p(29 * '-')
for _ in v:
p(' {0[0]:.6e}\t{0[1]:.6e}'.format(_))
p(29 * '-')
p('')
def to_mobility(rec):
for k in rec:
if k == 'PROCESS':
rec['PROCESS'] = 'Mobility x gas density (muN)'
if k == 'COLUMNS':
rec['COLUMNS'] = ('Reduced electric field (Td) | '
'Mobility x gas density ((m.V.s)-1)')
if k == 'DATA':
rec['DATA'] = [[_[0], _[1] / (_[0] * 1.0e-21)] for _ in rec['DATA']]
for rec in recs:
if rec['PROCESS'].startswith('Drift'):
to_mobility(rec)
with open('swarm_dutton_annex.txt', 'w') as fh:
for rec in recs:
print_record(rec, fh)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division
import re
from collections import OrderedDict
def load_bolsig(filename):
"""Load BOSIG+ output.
Parameters
----------
filename : str
File name.
Returns
-------
records : dict
Data.
"""
def _parse_record(fh, record_name):
while 1:
line = next(fh)
if line.startswith(record_name):
break
# There is 'Energy (eV)' in 'Rate coefficients' and 'Townsende
# coefficients' sections for some reason. Store the data temporarily
# and delete them afterwards.
if record_name in ('Rate coefficients', 'Townsend coefficients'):
names = ['E/N (Td)', 'Energy (eV)']
else:
names = ['E/N (Td)']
while 1:
line = next(fh)
if line.startswith(' '):
break
name = ' '.join(line.strip().split()[1:])
names.append(name)
columns = [[] for i in range(len(names))]
while 1:
line = next(fh)
if line.startswith(('\n', '\r\n')):
break
row = map(float, line.strip().split()[1:])
for v, c in zip(row, columns):
c.append(v)
record = OrderedDict(zip(names, columns))
return record
record_names = (
'Transport coefficients',
'Rate coefficients',
'Townsend coefficients',
)
with open(filename) as fh:
records = [_parse_record(fh, name) for name in record_names]
# Remove 'Evergy (eV)' entry from 'Rate coefficients' and 'Townsend
# coefficients' sections.
records[1].pop('Energy (eV)')
records[2].pop('Energy (eV)')
return dict(zip(record_names, records))
def load_swarm(filename):
"""Load swarm and transport data.
Parameters
----------
filename : str
File name.
Returns
-------
records : list
List of data records in dicts.
"""
def _parse_record(fh):
record = OrderedDict()
while 1:
line = next(fh)
if line.startswith('SPECIES'):
break
name, value = line.split(':', 1)
record[name] = value.strip()
while 1:
line = next(fh)
if line.startswith('-'):
break
name, value = line.split(':', 1)
record[name] = value.strip()
data = []
while 1:
line = next(fh)
if line.startswith('-'):
break
data.append(map(float, line.strip().split()))
record['DATA'] = data
return record
with open(filename) as fh:
records = []
while 1:
try:
records.append(_parse_record(fh))
except StopIteration:
break
return records
def load_xsec(filename):
"""Load cross sections data.
Parameters
----------
filename : str
File name.
Returens
--------
records : list
List of cross section data in dicts.
"""
def parse_record(fh):
processes = ('EXCITATION', 'ATTACHMENT', 'IONIZATION', 'EFFECTIVE',
'ELASTIC')
record = OrderedDict()
while 1:
line = next(fh)
if line.startswith(processes):
break
record['PROCESS'] = line.strip()
record['TARGET'] = next(fh).strip()
line = next(fh)
if not re.match('^\w', line):
vals = line.strip().split()
if vals[2] == 'threshold':
record['THRESHOLD'] = float(vals[0])
elif vals[2] == 'mass':
record['MASS RATIO'] = float(vals[0])
else:
raise ValueError('Can not parse the 3rd line.')
# Skip optional lines.
while 1:
line = next(fh)
if line.startswith('-'):
break
data = []
while 1:
line = next(fh)
if line.startswith('-'):
break
data.append(map(float, line.strip().split()))
record['DATA'] = data
return record
with open(filename, 'r') as fh:
records = []
while 1:
try:
records.append(parse_record(fh))
except StopIteration:
break
return records
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment