-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathltspice.go
225 lines (167 loc) · 3.98 KB
/
ltspice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package ltspice
import (
"bufio"
"encoding/binary"
"errors"
"io"
"log"
"math"
"os"
"strconv"
"strings"
"unicode/utf16"
)
// Raw reads an LTSpice raw (binary) file and returns a square matrix
// with the time in the first column, and data points in the rest.
// It also returns an array of column names
//
func Raw(file string) ([][]float64, []string, error) {
var row, col int
var fi *os.File
var err error
var vars []string
var buf = []byte{0, 0}
var lines []string
var xvii = false
// doubleVars is set if "Flags: double" is found (means that double's (64 bits)
// are written to the raw file instead of floats (32 bits)
doubleVars := false
if len(file) == 0 {
fi = os.Stdin
} else {
fi, err = os.Open(file)
if err != nil {
return nil, nil, err
}
defer fi.Close()
}
f := bufio.NewReader(fi)
// First identify if the raw file comes from LTSpice IV or XVII
//
// LTSpice IV has an ASCII header, while LTSpice XVII uses UTF16LE
i, err := f.Read(buf)
if err != nil {
return nil, nil, err
}
if i < 2 || buf[0] != 'T' {
return nil, nil, errors.New("Not an LTSpice RAW file")
}
if buf[1] == 0 {
xvii = true
}
// Read the text part
if xvii {
var uline []uint16
for {
n, _ := f.Read(buf)
if n != 2 {
return nil, nil, errors.New("premature end")
}
u := uint16(buf[0]) | uint16(buf[1]<<8)
uline = append(uline, u)
if buf[0] == 10 {
line := string(utf16.Decode(uline))
if line == "Binary:\n" {
break
}
lines = append(lines, line)
uline = nil
}
}
} else {
var b = []byte{0}
var bb []byte
for {
n, _ := f.Read(b)
if n != 1 {
return nil, nil, errors.New("premature end")
}
bb = append(bb, b[0])
if b[0] == 10 {
line := string(bb)
if line == "Binary:\n" {
break
}
lines = append(lines, line)
bb = nil
}
}
}
for i := 0; i < len(lines); i++ {
s := lines[i]
if strings.HasPrefix(s, "Flags:") {
flags := strings.Split(s[6:], " ")
for _, flag := range flags {
if flag == "double" {
doubleVars = true
log.Println("double vars")
} else if flag == "compressed" {
return nil, nil, errors.New("compressed RAWs not supported")
}
}
} else if strings.HasPrefix(s, "No. Variables:") {
col, _ = strconv.Atoi(strings.TrimSpace(s[14:]))
} else if strings.HasPrefix(s, "No. Points:") {
row, _ = strconv.Atoi(strings.TrimSpace(s[11:]))
} else if strings.HasPrefix(s, "Variables:") {
for j := 0; j < col; j++ {
i++
s = lines[i]
ss := strings.Split(s, "\t")
vars = append(vars, ss[2])
}
}
}
// Read the binary part into an array
Ma := make([]float64, row*col)
// use slices to cover the array
M := make([][]float64, col)
for i := range M {
M[i] = Ma[i*row : (i+1)*row]
}
t := make([]byte, 8)
v4 := make([]byte, 4)
v8 := make([]byte, 8)
for i := 0; i < row; i++ {
// Read a time stamp (8 bytes floating point)
// [!] r.Read() may not read len(buf) bytes even if there are left.
_, err = io.ReadFull(f, t)
if err != nil {
log.Println("unexpected end")
return nil, nil, err
}
M[0][i] = toFloat(t)
for j := 1; j < col; j++ {
if doubleVars {
// Read a data point (8 bytes floating point)
_, err = io.ReadFull(f, v8)
if err != nil {
log.Println("unexpected end")
return nil, nil, err
}
M[j][i] = toFloat(v8)
} else {
// Read a data point (4 bytes floating point)
_, err = io.ReadFull(f, v4)
if err != nil {
log.Println("unexpected end")
return nil, nil, err
}
M[j][i] = toFloat32(v4)
}
}
}
return M, vars, nil
}
// toFloat converts an array of 8 bytes to a float64 value.
func toFloat(bytes []byte) float64 {
bits := binary.LittleEndian.Uint64(bytes)
float := math.Float64frombits(bits)
return float
}
// toFloat32 converts an array of 4 bytes to a float64 value.
func toFloat32(bytes []byte) float64 {
bits := binary.LittleEndian.Uint32(bytes)
float := math.Float32frombits(bits)
return float64(float)
}