Skip to content

Instantly share code, notes, and snippets.

@fakuivan
Last active October 11, 2019 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fakuivan/1a3ca8ea630c04bf259470f7662a5488 to your computer and use it in GitHub Desktop.
Save fakuivan/1a3ca8ea630c04bf259470f7662a5488 to your computer and use it in GitHub Desktop.
Simple CSV parser function and example plotter program compatible with _some_ RIGOL oscilloscopes
#!/usr/bin/env python3
from oscilloscope import parse_csv_export, ParsedCSV
import argparse
from io import TextIOWrapper
import matplotlib.pyplot as plt
def main():
parser = argparse.ArgumentParser(description="Example plotter")
parser.add_argument("infile", type=argparse.FileType('r'))
file: TextIOWrapper = parser.parse_args().infile
data = parse_csv_export(file)
plt.plot(*list(map(list, zip(*data.points))))
plt.xlabel(f"{data.axis_names[0]} ({data.axis_units[0]})")
plt.ylabel(f"{data.axis_names[1]} ({data.axis_units[1]})")
plt.show()
if __name__ == "__main__":
main()
#!/usr/bin/env python3
from io import TextIOWrapper
from typing import Tuple, List, NamedTuple
import csv
class ParsedCSV(NamedTuple):
axis_names: Tuple[str, str]
axis_units: Tuple[str, str]
points: List[Tuple[float, float]]
def parse_csv_export(file: TextIOWrapper) -> ParsedCSV:
bad_format = ValueError("File is not a valid oscilloscope export")
table = csv.reader(file, dialect="unix")
table_iter = iter(table)
names = tuple(next(table_iter)[0:-1])
units = tuple(next(table_iter)[0:-1])
data = list()
if len(names) != 2 or len(units) != 2:
raise bad_format
for row in table_iter:
try:
pair = tuple(map(float, row[0: -1]))
except ValueError:
raise bad_format
if len(pair) != 2:
raise bad_format
data.append(pair)
return ParsedCSV(names, units, data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment