Created
July 31, 2021 20:06
-
-
Save anglyan/33c7536fd0b9f19fbfa33d45da5bafcd to your computer and use it in GitHub Desktop.
Simple script to convert printed output from traditional SPICE simulators into a single csv file / dataframe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Simple script to convert printed output from traditional | |
SPICE simulators into a single csv file / dataframe. | |
Usage | |
----- | |
python spice2csv.py output.prn | |
Use: | |
python spice2csv.py -h | |
for info about the options. | |
""" | |
import sys | |
def merge_tables(tables): | |
table0 = tables[0] | |
for i, row in enumerate(table0): | |
for t in tables[1:]: | |
row.extend(t[i][2:]) | |
return table0 | |
def print_table(table, sep=",", add_comment=False): | |
header = table[0] | |
if add_comment: | |
print("#" + sep.join(header)) | |
else: | |
print(sep.join(header)) | |
for row in table[1:]: | |
print(sep.join(row)) | |
if __name__ == "__main__": | |
import argparse | |
parser = argparse.ArgumentParser("Process ngspice output files") | |
parser.add_argument('inputfile', metavar="N", | |
type=str, nargs=1, help="input file") | |
parser.add_argument("--sep", default=",") | |
parser.add_argument("--comm", action='store_true') | |
args = parser.parse_args() | |
tables = [] | |
in_table = False | |
ignore = 0 | |
with open(args.inputfile[0], "r") as f: | |
lines = f.readlines() | |
for i, line in enumerate(lines): | |
if ignore > 0: | |
ignore -= 1 | |
continue | |
if not in_table: | |
if line.startswith("Index"): | |
table = [] | |
in_table = True | |
headers = line.strip().split() | |
table.append(headers) | |
ignore = 1 | |
else: | |
if line.startswith("\f"): | |
if lines[i+1].startswith("Index"): | |
ignore = 2 | |
continue | |
else: | |
in_table = False | |
tables.append(table) | |
else: | |
data = line.strip().split() | |
if len(data) == 0: | |
tables.append(table) | |
in_table =False | |
else: | |
table.append(data) | |
data = ",".join(data) | |
merged = merge_tables(tables) | |
print_table(merged, sep=args.sep) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment