Debian Codegraph @

scripts/misc/scp_info.py

raw symbols SCIP
1 import struct, sys
2 import matplotlib.pyplot as plt
3
4 NO_DAT = 0
5 PRINT_DAT = 1
6 PLOT_DAT = 2
7
8 def decode_flux(tdat):
9 flux, fluxl = 0, []
10 while tdat:
11 f, = struct.unpack(">H", tdat[:2])
12 tdat = tdat[2:]
13 if f == 0:
14 flux += 65536
15 else:
16 flux += f
17 fluxl.append(flux / 40)
18 flux = 0
19 return fluxl
20
21 def dump_track(dat, trk_offs, trknr, show_dat):
22 print("Track %u:" % trknr)
23
24 trk_off = trk_offs[trknr]
25 if trk_off == 0:
26 print("Empty")
27 return
28
29 # Parse the SCP track header and extract the flux data.
30 thdr = dat[trk_off:trk_off+4+12*nr_revs]
31 sig, tnr, _, _, s_off = struct.unpack("<3sB3I", thdr[:16])
32 assert sig == b"TRK"
33 assert tnr == trknr
34 p_idx, tot = [], 0.0
35 for i in range(nr_revs):
36 t,n,off = struct.unpack("<3I", thdr[4+i*12:4+(i+1)*12])
37 flux = decode_flux(dat[trk_off+off:trk_off+off+n*2])
38 print("Rev %u: time=%.2fus nr_flux=%u tot_flux=%.2fus"
39 % (i, t/40, n, sum(flux)))
40 tot += t/40
41 p_idx.append(tot)
42 if not show_dat:
43 return
44
45 _, e_nr, e_off = struct.unpack("<3I", thdr[-12:])
46 fluxl = decode_flux(dat[trk_off+s_off:trk_off+e_off+e_nr*2])
47 tot = 0.0
48 i = 0
49 px, py = [], []
50 for x in fluxl:
51 if show_dat == PRINT_DAT:
52 bad = ""
53 if (x < 3.6) or ((x > 4.4) and (x < 5.4)) \
54 or ((x > 6.6) and (x < 7.2)) or (x > 8.8):
55 bad = "BAD"
56 print("%d: %f %s" % (i, x, bad))
57 elif x < 50:
58 px.append(tot/1000)
59 py.append(x)
60 i += 1
61 tot += x
62 print("Total: %uus (%uus per rev)" % (int(tot), tot//nr_revs))
63 if show_dat == PLOT_DAT:
64 plt.xlabel("Time (ms)")
65 plt.ylabel("Flux (us)")
66 plt.gcf().set_size_inches(12, 8)
67 plt.axvline(x=0, ymin=0.95, color='r')
68 for t in p_idx:
69 plt.axvline(x=t/1000, ymin=0.95, color='r')
70 plt.scatter(px, py, s=1)
71 plt.ylim((0, None))
72 plt.show()
73
74 argv = sys.argv
75
76 plot = (argv[1] == '--plot')
77 if plot:
78 argv = argv[1:]
79
80 with open(argv[1], "rb") as f:
81 dat = f.read()
82
83 header = struct.unpack("<3s9BI", dat[0:16])
84 (sig, _, _, nr_revs, s_trk, e_trk, flags, _, ss, _, _) = header
85 assert sig == b"SCP"
86 nr_sides = 1 if ss else 2
87
88 trk_offs = struct.unpack("<168I", dat[16:0x2b0])
89
90 print("Revolutions: %u" % nr_revs)
91
92 if len(argv) == 3:
93 dump_track(dat, trk_offs, int(argv[2]), PLOT_DAT if plot else PRINT_DAT)
94 else:
95 for i in range(s_trk, e_trk+1):
96 dump_track(dat, trk_offs, i, NO_DAT)
97