Read ngspice binary raw files. Return tuple of the data, and the plot metadata. The dtype of the data contains field names. This is not very robust yet, and only supports ngspice. >>> darr, mdata = rawread('test.py') >>> darr.dtype.names >>> plot(np.real(darr['frequency']), np.ab
(fname: str)
| 7 | b'no. points', b'dimensions', b'command', b'option'] |
| 8 | |
| 9 | def rawread(fname: str): |
| 10 | """Read ngspice binary raw files. Return tuple of the data, and the |
| 11 | plot metadata. The dtype of the data contains field names. This is |
| 12 | not very robust yet, and only supports ngspice. |
| 13 | >>> darr, mdata = rawread('test.py') |
| 14 | >>> darr.dtype.names |
| 15 | >>> plot(np.real(darr['frequency']), np.abs(darr['v(out)'])) |
| 16 | """ |
| 17 | # Example header of raw file |
| 18 | # Title: rc band pass example circuit |
| 19 | # Date: Sun Feb 21 11:29:14 2016 |
| 20 | # Plotname: AC Analysis |
| 21 | # Flags: complex |
| 22 | # No. Variables: 3 |
| 23 | # No. Points: 41 |
| 24 | # Variables: |
| 25 | # 0 frequency frequency grid=3 |
| 26 | # 1 v(out) voltage |
| 27 | # 2 v(in) voltage |
| 28 | # Binary: |
| 29 | fp = open(fname, 'rb') |
| 30 | plot = {} |
| 31 | count = 0 |
| 32 | arrs = [] |
| 33 | plots = [] |
| 34 | while (True): |
| 35 | try: |
| 36 | mdata = fp.readline(BSIZE_SP).split(b':', maxsplit=1) |
| 37 | except: |
| 38 | raise |
| 39 | if len(mdata) == 2: |
| 40 | if mdata[0].lower() in MDATA_LIST: |
| 41 | plot[mdata[0].lower()] = mdata[1].strip() |
| 42 | if mdata[0].lower() == b'variables': |
| 43 | nvars = int(plot[b'no. variables']) |
| 44 | npoints = int(plot[b'no. points']) |
| 45 | plot['varnames'] = [] |
| 46 | plot['varunits'] = [] |
| 47 | for varn in range(nvars): |
| 48 | varspec = (fp.readline(BSIZE_SP).strip() |
| 49 | .decode('ascii').split()) |
| 50 | assert(varn == int(varspec[0])) |
| 51 | plot['varnames'].append(varspec[1]) |
| 52 | plot['varunits'].append(varspec[2]) |
| 53 | if mdata[0].lower() == b'binary': |
| 54 | rowdtype = np.dtype({'names': plot['varnames'], |
| 55 | 'formats': [np.complex_ if b'complex' |
| 56 | in plot[b'flags'] |
| 57 | else np.float_]*nvars}) |
| 58 | # We should have all the metadata by now |
| 59 | arrs.append(np.fromfile(fp, dtype=rowdtype, count=npoints)) |
| 60 | plots.append(plot) |
| 61 | fp.readline() # Read to the end of line |
| 62 | else: |
| 63 | break |
| 64 | return (arrs, plots) |
| 65 | |
| 66 | if __name__ == '__main__': |
no test coverage detected