Skip to content

Instantly share code, notes, and snippets.

@aerialist
Last active April 25, 2020 03:08
Show Gist options
  • Save aerialist/907c2fa3064c9b699ce5bb1ed325eb92 to your computer and use it in GitHub Desktop.
Save aerialist/907c2fa3064c9b699ce5bb1ed325eb92 to your computer and use it in GitHub Desktop.
Read csv file from ADALM2000 Scopy and create Python Pandas DataFrame
import pandas as pd
import datetime
def make_dt(timestr, start_dt):
"""
Take starting datetime object and time stamp string in '23:20:35' format
Return a datetime object with date info from starting datetime and time info from time stamp string
"""
ts = datetime.datetime.strptime(timestr, "%H:%M:%S")
return datetime.datetime(start_dt.year, start_dt.month, start_dt.day, ts.hour, ts.minute, ts.second)
def add_day(td):
"""
Measurement may happen overnight.
Add a day if time delta is negative
"""
while td.total_seconds() < 0:
td += datetime.timedelta(hours=24)
return td
def get_startdt_scopy_csv(fname):
"""
Print out comment lines
For log file from DMM, parse starting date&time and return DateTime object
"""
startdt = None
with open(fname) as f:
for line in f.readlines():
if line[0] == ";":
print(line, end='')
if line.startswith(";Started on "):
# log from DMM has a line with starting date&time
_, timestr = line.strip().split(";Started on ")
startdt = datetime.datetime.strptime(timestr, "%a %b %d %H:%M:%S %Y")
return startdt
def read_scopy_csv(fname):
"""
Read csv file from Scopy and return as pandas.DataFrame
Export from oscilloscope is just a simple reading
Log from DMM adds a few columns
"""
startdt = get_startdt_scopy_csv(fname)
df = pd.read_csv(fname, comment=";")
if startdt != None:
# log file from DMM
df.loc[:, 'datetime'] = df['Timestamp'].apply(make_dt, args=(startdt,))
df.loc[:, 'timedelta'] = df['datetime'] - startdt
df.loc[:, 'timedelta'] = df['timedelta'].apply(add_day)
df.loc[:, 'elapsed time (s)'] = df['timedelta'].apply(lambda x: x.total_seconds())
return df
if __name__ == "__main__":
# Test log file from DMM
fname = "scopy_dmm_log.csv"
df = read_scopy_csv(fname)
print(df.head())
# Test export file from Oscilloscope
fname = "scopy_oscilloscope_export.csv"
df = read_scopy_csv(fname)
print(df.head())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment