1 from __future__
import with_statement
3 from oscopy
import Signal
4 from reader
import Reader
, ReadError
7 class Spice2rawReader(Reader
):
8 """ Read Berkeley Spice2G6 'raw' output files
10 Currently support only one dataset per file
11 Date and Time fields are not processed
13 see http://www.rvq.fr/linux/gawfmt.php for format description
15 _signature
= 'rawfile1'
16 _types_to_unit
= {0: 'a.u.', 1: 's', 2: 'V', 3: 'A', 4: 'Hz', 5: 'a.u.'}
18 """ Look at the header if it contains the keyword self._signature
23 Path to the file to test
28 True if the file can be handled by this reader
37 return s
== self
._signature
39 def _read_signals(self
):
40 """ Read the signals from the file
42 Supports only one dataset per file
52 The list of Signals read from the file
57 In case of invalid path or unsupported file format
59 header_fields
= ['signature', 'title', 'date', 'time',
60 'mode', 'nvars', 'const4']
61 with
open(self
._fn
) as f
:
64 title
= f
.read(80).strip('\x00')
66 (date
, t
, mode
, nvars
, const4
) = struct
.unpack('<2d3h' ,res
)
67 self
._info
.update(dict(zip(header_fields
, (signature
, title
, date
, t
, mode
, nvars
, const4
))))
70 for i
in xrange(nvars
):
71 names
.append(f
.read(8).strip('#').strip('\x00'))
72 types
= struct
.unpack('<%dh' % nvars
, f
.read(2 * nvars
))
73 locs
= struct
.unpack('<%dh' % nvars
, f
.read(2 * nvars
))
74 self
._info
['plottitle'] = f
.read(24).strip('\x00')
76 # Now we can create the signals
78 for i
in xrange(nvars
):
79 signals
.append(Signal(names
[i
],
80 self
._types
_to
_unit
.get(types
[i
], 'a.u.')))
82 data
= [[] for x
in xrange(len(names
))]
83 # optimization: cache the append methods,
84 # avoiding one dictionary lookup per line
85 append
= [x
.append
for x
in data
]
87 tmp
= f
.read(8 * nvars
)
88 if len(tmp
) < 8 * nvars
: break
89 values
= struct
.unpack('<%dd' % nvars
, tmp
)
90 for i
, v
in enumerate(values
):
95 for i
, s
in enumerate(signals
[1:]):
99 self
._signals
= dict(zip(names
[1:], signals
[1:]))