Commit 6c07897f authored by Peter Jansweijer's avatar Peter Jansweijer

Oscilloscope wafeform files are partly binary. On windows machines the files…

Oscilloscope wafeform files are partly binary. On windows machines the files must be opened in mode "rb" and proper utf-8 decoding needs to be in place.
parent c0a2e645
......@@ -340,64 +340,64 @@ def get_preamble_from_file(data_file, date_in_file, time_in_file):
y_min_grid_found = False
while 1:
line=data_file.readline()
line=data_file.readline().decode("utf-8")
if len(line) == 0:
Exception("get_preamble_from_file: premature end of file.")
break
if line.strip() == "#preamble_end:":
if "#preamble_end:" in line:
break
line_lst = line.split(":")
#print("ll:",line_lst)
if line_lst[0].strip() == "COMM_TYPE": # Either: "BYTE", "WORD"
if "COMM_TYPE" in line_lst[0].strip(): # Either: "BYTE", "WORD"
_preamble["format"] = line_lst[1].strip().upper()
_preamble["type"]="RAW"
if line_lst[0].strip() == "WAVE_ARRAY_COUNT":
if "WAVE_ARRAY_COUNT" in line_lst[0].strip():
_preamble["points"] = int(line_lst[1])
if line_lst[0].strip() == "SWEEPS_PER_ACQ":
if "SWEEPS_PER_ACQ" in line_lst[0].strip():
_preamble["count"] = int(line_lst[1])
if line_lst[0].strip() == "HORIZ_INTERVAL":
if "HORIZ_INTERVAL" in line_lst[0].strip():
_preamble["x_increment"] = float(line_lst[1])
if line_lst[0].strip() == "HORIZ_OFFSET": # Don't know which one to choose
#if line_lst[0].strip() == "PIXEL_OFFSET":
if "HORIZ_OFFSET" in line_lst[0].strip(): # Don't know which one to choose
#if "PIXEL_OFFSET" in line_lst[0].strip():
_preamble["x_origin"] = float(line_lst[1])
if line_lst[0].strip() == "FIRST_POINT":
if "FIRST_POINT" in line_lst[0].strip():
_preamble["x_reference"] = float(line_lst[1])
if line_lst[0].strip() == "VERTICAL_GAIN":
if "VERTICAL_GAIN" in line_lst[0].strip():
_preamble["y_increment"] = float(line_lst[1])
if line_lst[0].strip() == "VERTICAL_OFFSET":
if "VERTICAL_OFFSET" in line_lst[0].strip():
_preamble["y_origin"] = float(line_lst[1])
#if line_lst[0].strip() == "VERTICAL_VERNIER":
#if "VERTICAL_VERNIER" in line_lst[0].strip():
# _preamble["y_reference"]=float(line_lst[1])
_preamble["y_reference"]=float(0) # Not sure which LeCroy parameter to take...
if line_lst[0].strip() == "VERT_COUPLING": # Either:"AC_1MOhm", "DC_1MOhm", "DC_50_Ohms", "ground"
if "VERT_COUPLING" in line_lst[0].strip(): # Either:"AC_1MOhm", "DC_1MOhm", "DC_50_Ohms", "ground"
_preamble["coupling"] = line_lst[1].strip()
if line_lst[0].strip() == "PIXEL_OFFSET":
if "PIXEL_OFFSET" in line_lst[0].strip():
_preamble["x_display_origin"] = float(line_lst[1])
if line_lst[0].strip() == "TIMEBASE":
if "TIMEBASE" in line_lst[0].strip():
_preamble["x_display_range"]=float(10 * float(_timebase[line_lst[1].strip()]))
if line_lst[0].strip() == "MAX_VALUE":
if "MAX_VALUE" in line_lst[0].strip():
y_max_grid = float(line_lst[1])
y_max_grid_found = True
if y_min_grid_found:
_preamble["y_display_range"] = y_max_grid - y_min_grid
_preamble["y_display_origin"] = (y_max_grid - y_min_grid) / 2
if line_lst[0].strip() == "MIN_VALUE":
if "MIN_VALUE" in line_lst[0].strip():
y_min_grid = float(line_lst[1])
y_min_grid_found = True
if y_max_grid_found:
......@@ -407,18 +407,18 @@ def get_preamble_from_file(data_file, date_in_file, time_in_file):
_preamble["date"]=str(date_in_file)
_preamble["time"]=str(time_in_file)
if line_lst[0].strip() == "INSTRUMENT_NAME":
if "INSTRUMENT_NAME" in line_lst[0].strip():
_preamble["frame_model_#"]=line_lst[1].strip()
if line_lst[0].strip() == "RECORD_TYPE":
if "RECORD_TYPE" in line_lst[0].strip():
_preamble["acquisition_mode"]=line_lst[1].strip()
_preamble["completion"]=int(0) # assumed not to be used
if line_lst[0].strip() == "HORUNIT":
if "HORUNIT" in line_lst[0].strip():
_preamble["x_units"]=line_lst[1].strip()
if line_lst[0].strip() == "VERTUNIT":
if "VERTUNIT" in line_lst[0].strip():
_preamble["y_units"]=line_lst[1].strip()
_preamble["bandwidth_maximum"]=float(0) # assumed not to be used
......@@ -572,21 +572,24 @@ def file_to_waveform(filename):
}
"""
data_file = open(filename,"r")
# Open data file for "read", in "binary" format
# Then readline() returns type <bytes> that can be
# decoded using "utf-8" into string
data_file = open(filename,"rb")
# create an empty wavefrom_data dictionairy
waveform_data = {}
line = data_file.readline()
if line.strip() != "#WaveformData:LeCroy8254":
line = data_file.readline().decode("utf-8")
if "#WaveformData:LeCroy8254" not in line:
#print("Exception: file_to_waveform: Not a LeCroy8254 Waveform Data file.")
Exception("file_to_waveform: Not a LeCroy8254 Waveform Data file.")
data_file.close()
return
line = data_file.readline()
line = data_file.readline().decode("utf-8")
version = line.strip().split(":")
if not(version[0]=="#version" and version[1]=="0.2"):
if not(("#version" in version[0]) and ("0.2" in version[1])):
# if version[0]=="#version" and version[1]=="0.1":
#print("Exception: file_to_waveform: LeCroy8254 wrong version Waveform Data file.")
Exception("file_to_waveform: LeCroy8254 wrong version Waveform Data file.")
......@@ -594,36 +597,42 @@ def file_to_waveform(filename):
return
while 1:
# Note that readline() scans the file in "binary" mode.
# Some readline actions should be kept binary (the raw
# waveform data) and some should be decoded using "utf-8"
line=data_file.readline()
if len(line)==0:
break
if line[:len("#date:")]=="#date:":
date_in_file=line.split(":")[1].strip()
if line[:len("#time:")]=="#time:":
time_lst=line.split(":")
if "#date:" in str(line):
date_in_file=line.decode("utf-8").split(":")[1].strip()
if "#time:" in str(line):
time_lst=line.decode("utf-8").split(":")
time_in_file=time_lst[1].strip()+":"+time_lst[2].strip()+":"+time_lst[3].strip()
if line[:len("#byteorder:")]=="#byteorder:":
byte_order=line.split(":")[1].strip()
if line[:len("#preamble:")]=="#preamble:":
line=data_file.readline() # read something like "C1:INSP "
chan_no = int(line[1]) # channel number is the second character
if "#byteorder:" in str(line):
byte_order = line.decode("utf-8").split(":")[1].strip()
if "#preamble:" in str(line):
line = data_file.readline().decode("utf-8") # read something like "C1:INSP "
chan_no = int(line[line.index('C')+1]) # channel number is the character after "C"
# Preambles come first in the file.
# Create a dictionairy channel entry for each
waveform_data[chan_no] = {}
waveform_data[chan_no]["byte_order"] = byte_order
waveform_data[chan_no]["preamble"] = \
preamble = get_preamble_from_file(data_file, date_in_file, time_in_file)
if line[:len("#waveform_data:")]=="#waveform_data:":
#print(preamble)
if "#waveform_data:" in str(line):
# the next part of the file looks like this:
# C1:WF DAT1,#9000000100e\8E....
wf_header = data_file.read(12) # read the waveform_header "C1:WF DAT1,#"
chan_no = int(wf_header[1])
wf_header = str(wf_header)
chan_no = int(wf_header[wf_header.index('C')+1])
wf_no_of_digits = int(data_file.read(1)) # read the number of digits "9"
wf_samples = int(data_file.read(wf_no_of_digits))
#print("chan_no",chan_no)
#print(wf_header)
#print(wf_no_of_digits)
#print(wf_samples)
#print(wf_samples)
#print(byte_order)
# Add the waveform to the <type 'dict'> waveform_data but first convert it the RAW
# waveform data in the file to scipy array with x,y axis values
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment