forked from gpasquev/isfread-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTEK_isfread.py
193 lines (148 loc) · 6.25 KB
/
TEK_isfread.py
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
#!/usr/bin/env python
#coding: utf-8
from __future__ import absolute_import
from __future__ import print_function
import struct
from six.moves import range
from io import open
""" Python script for read data from a Tektronix ".ISF" files.
Python script for reading binary ISF files from TDS series Tektronix
Oscilloscope instruments.
SCRIPT:
=======
It can be run as an script from the command line converting the ISF file into a
two columns ASCII file:
$python isfread.py blablabla.isf > outputfile.dat
PYTHON MODULE:
==============
This module contains the function isfread which expect the name of the ISF file
an unique input argument and retrive a tuple with the x-y columns and a
dictionary with the head information.
----------------------------------------------------------------------------
About ISF files and this code
=============================
This code is base on the similar code for Matlab isfread.m from John
Lipp [Lipp]. Information on ISF-file is detailed in the Tektronix "Programmer
Manual" [TekMan].
References:
[Lipp] John Lipp - isfread.m matlab code.
www.mathworks.com/matlabcentral/fileexchange/6247
[TekMan] Tektronix Manuals, "Programmer Manual. TDS3000, TDS3000B, and
TDS3000C Series Digital Phosphor Oscilloscopes", 071-0381-03.
www.tektronix.com
Gustavo Pasquevich'sisfread [https://github.com/gpasquev/isfread-py]
---------------------------------------------------------------------------
Contact: Gustavo Pasquevich, Universidad Nacional de La Plata - Argentina
"""
import struct
__version__= '0.31'
__author__= 'Gustavo Pasquevich (2011), fenixnano(2019)'
__email__= '[email protected], [email protected]'
def cmp(a,b):
return ((a > b) - (a < b))
def isfread(filename):
""" Read isf file and return x y and head information.
input:
string with the ISF-filename.
output:
Returns a tuple of three elements:
x - list with the x values
y - list with the y values
head - dictionary with the head-information stored in the file."""
FID = open(filename,'r', encoding="latin-1")
hdata = FID.read(511); # read first 511 bytes
# Subroutines used to extract inrormation from the head --------------------
def getnum(string,tag):
""" Loock into the string for the tag and extract the concequent number"""
n1=string.find(tag)
n2=string.find(';',n1)
s2=string[n1+len(tag):n2]
j=s2.find('.')
if j==-1:
return int(string[n1+len(tag):n2])
else:
return float(string[n1+len(tag):n2])
def getstr(string,tag):
""" Loock into the string for the tag and extract the concequent string"""
n1=string.find(tag)
n2=string.find(';',n1)
return string[n1+len(tag):n2].lstrip()
def getquotedstr(string,tag):
""" Loock into the string for the tag and extract the concequent quoted
string"""
n1=string.find(tag)
n2=string.find('"',n1+1)
n3=string.find('"',n2+1)
return string[n2+1:n3]
#---------------------------------------------------------------------------
head={'bytenum': getnum(hdata,'BYT_NR'),
'bitnum': getnum(hdata,'BIT_NR'),
'encoding': getstr(hdata,'ENCDG'),
'binformat': getstr(hdata,'BN_FMT'),
'byteorder': getstr(hdata,'BYT_OR'),
'wfid': getquotedstr(hdata,'WFID'),
'pointformat': getstr(hdata,'PT_FMT'),
'xunit': getquotedstr(hdata,'XUNIT'),
'yunit': getquotedstr(hdata,'YUNIT'),
'xzero': getnum(hdata,'XZERO'),
'xincr': getnum(hdata,'XINCR'),
'ptoff': getnum(hdata,'PT_OFF'),
'ymult': getnum(hdata,'YMULT'),
'yzero': getnum(hdata,'YZERO'),
'yoff': getnum(hdata,'YOFF'),
'npts': getnum(hdata,'NR_PT')}
# The only cases that this code (at this moment) not take into acount.
if ((head['bytenum'] != 2) or (head['bitnum'] != 16) or
(cmp(head['encoding'],'BIN') and cmp(head['encoding'],'BINARY')) or cmp(head['binformat'],'RI') or
cmp(head['pointformat'],'Y')):
FID.close()
print('Unable to process IFS file.')
# Reading the <Block> part corresponding to the "CURVe" command [TekMan].
# <Block> = ":CURVE #<x><yy..y><data>"
# <x> number of bytes defining <yy..y>
# <yy..y> number of bytes to "transfer"/read in the data part.
# <data>: the data in binary
#
# Comment: It should be happend that: NR_PT times BYT_NR = <yy..y>
# Skipping the #<x><yy...y> part of the <Block> bytes
ii = hdata.find(':CURVE #')
FID.seek(ii+8)
skip = int(FID.read(1))
n1 = int(FID.read(skip))
# information from the head needed to read and to convert the data
npts = head['npts']
yzero= head['yzero']
ymult= head['ymult']
xzero= head['xzero']
xincr= head['xincr']
ptoff= head['ptoff']
yoff = head['yoff']
dict_endian = { # Dictionary to converts significant bit infor-
'MSB': '>', # mation to struct module definitions.
'LSB': '<'
}
fmt = dict_endian[head['byteorder']] + str(npts) + 'h'
n2=struct.calcsize(fmt)
# "n1" is the number of bytes to be readed directly from Tek-ISF-file.
# Meanwhile "n2" is the number of bytes to be readed calculated through:
# NumOfPoints x BytePerPoint
if n1 != n2:
print("WARNING: Something is not going as is was planned!!!")
FID2 = open(filename,'rb')
string_data=FID2.read(n2)
data=struct.unpack(fmt,string_data)
# Absolute values of data obtained as is defined in [Tek-Man] WFMPre:PT_Fmt
# command description.
v=[yzero + ymult*(y-yoff) for y in data]
x=[xzero + xincr*(i-ptoff) for i in range(npts)]
FID.close()
return x,v,head
isfread('MDO_20191127_233635_50KHZ3.0VPPSQUstart.isf')
if __name__ == "__main__":
import sys
filein = sys.argv[1]
x,v,head=isfread(filein)
print(head)
for i in range(len(x)):
print('%g %g'%(x[i],v[i]))