Skip to content

Commit

Permalink
Bug fix in CSV generation.
Browse files Browse the repository at this point in the history
Add timestamp to the plots
  • Loading branch information
sergei committed Oct 13, 2023
1 parent 6e85489 commit 5e747c5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
30 changes: 24 additions & 6 deletions scripts/plot_instr.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import argparse

import matplotlib.pyplot as plt
import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as md


def proces_logs(args):
Expand All @@ -18,7 +19,7 @@ def proces_logs(args):

# Heading data
process_sources(['heading-Precision-9', 'heading-ZG100'],
['mag', 'mag-cog'],
['mag', 'cog', 'mag-cog'],
args.work_dir)


Expand All @@ -33,13 +34,23 @@ def plot_values(sources, values, vals):
for source in sources:
label = source
val = np.array(vals[source][value])
timestamps = np.array(vals[source][value + '-time']) / 1000.0
dates = [dt.datetime.fromtimestamp(ts) for ts in timestamps]

if np.isnan(val).all():
continue

plt.xticks(rotation=25)
ax = plt.gca()
xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)

if '-' in value:
mean = np.nanmean(val)
std = np.nanstd(val)
label += f' {mean:.2f} +/- {std:.2f}'
plt.plot(val, label=label)
plt.plot(dates, val, label=label)

plt.legend()
plt.title(value)
plt.grid(True)
Expand All @@ -50,17 +61,20 @@ def read_values(sources, values, work_dir):
for source in sources:

vals[source] = {}
vals[source]['time'] = []
for value in values:
vals[source][value] = []
vals[source][value + '-time'] = []

csv_file_name = work_dir + '/' + source + '-instr.csv'
with open(csv_file_name, 'r') as f:
print(f'Reading {csv_file_name}')
for line in f:
t = line.split(',')
for value in values:
time_stamp_ms = int(t[0])
if '-' in value:
exp = value.split('-')
exp = value.split('-') # e.g. mag-cog compute difference between two values
op1 = exp[0]
op2 = exp[1]
else:
Expand All @@ -70,18 +84,22 @@ def read_values(sources, values, work_dir):
v = t[i + 1]
if t[i] == value:
val = float(v) if len(v) > 0 else np.nan
vals[source][value].append(val)
if op1 is not None and t[i] == op1:
val1 = float(v) if len(v) > 0 else np.nan
if op2 is not None and t[i] == op2:
val2 = float(v) if len(v) > 0 else np.nan

if op1 is not None and op2 is not None:
diff = val1 - val2
if diff > 180:
diff -= 360
elif diff < -180:
diff += 360
vals[source][value].append(diff)
else:
vals[source][value].append(val)

vals[source][value + '-time'].append(time_stamp_ms)

print(f'Processed {len(vals[source][values[0]])} values')
return vals
Expand Down
6 changes: 1 addition & 5 deletions ydn2csv/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ void makeNewPgnCsvFile(const std::string &oldPgnCsvFile, const std::string &newP
if ( tokens.size() > 3 ){
uint32_t pgn = std::stoul(tokens[0]);
std::string desc = tokens[1];
std::vector<std::string> srcs;
uint32_t srcIdx = std::stoul(tokens[2]);
for (int i = 3; i < tokens.size(); i++){
srcs.push_back(tokens[i]);
}
if( pgn == pgnToReplace ){
// Replace with the line that has this device only
fout << pgn << "," << desc << "," << 0 << "," << deviceName << std::endl;
Expand Down Expand Up @@ -73,6 +68,7 @@ int main(int argc, char** argv) {
std::string stYdvrDir = result["ydvr-dir"].as<std::string>();
std::string stCacheDir = result["cache-dir"].as<std::string>();
std::string stPgnCsvFile = result["pgn-src"].as<std::string>();
std::filesystem::create_directories(stCacheDir);

Source sources[] = {
// Speed over water
Expand Down

0 comments on commit 5e747c5

Please sign in to comment.