Tektronix Binary File Loader

Average rating
(0 votes)

The following function loads a binary data file exported from a Tektronix digital oscilloscope. Not extensively tested, and by no means fully supportive of every feature their binary format can support, but for a basic single channel file, I think this will work for any model. The loaded wave will be named after the file name, with the extension stripped off. If an empty string is passed for filenamestr, and open file dialog will be called.

function loadTEKbinary(filenamestr)
	string filenamestr
	variable fileref
	if (strlen(filenamestr))
		Open  /R /T=".isf" fileref as filenamestr
	else
		Open  /R /T=".isf" fileref
	endif
	if (!(strlen(S_filename)))
		print "Cancelled"
		return 0
	endif
//	string shortname = S_filename[StrSearchBack(S_filename, ":") + 1, strlen(S_filename) - 5]
	string shortname = S_filename[strsearch(S_filename, ":",Inf,1) + 1, strlen(S_filename) - 5]
	string preamble,curvestring
	FreadLine /N=8 fileref, preamble
	if (!stringmatch(preamble, ":WFMPRE:" ))
		print "Missing preamble"
	endif
	FreadLine /T=":" fileref, preamble
//	print preamble
//	The Tek form preamble can be used as a keyword string list with list separator ";" and keyseparator " "
 
	variable delx,ptoff,xzero,ymult,yoff,yzero,sizeitemsnum,numbytes,byteorder,bytetype
	variable numitems
 
	delx = NumberByKey("XINCR", preamble," ",";")
	ptoff = NumberByKey("PT_OFF", preamble," ",";")
	xzero = NumberByKey("XZERO", preamble," ",";")
 
	ymult = NumberByKey("YMULT", preamble," ",";")
	yoff = NumberByKey("YOFF", preamble," ",";")
	yzero = NumberByKey("YZERO", preamble," ",";")
	FreadLine /N=7 fileref, curvestring
	if (!stringmatch(curvestring, "CURVE #" ))
		print "Missing Binary Start String, 'CURVE #'"
		return 0
	endif
	FreadLine /N = 1 fileref, curvestring
	sizeitemsnum = str2num(curvestring)
	FreadLine /N = (sizeitemsnum) fileref, curvestring
	numbytes = str2num(curvestring)
 
 
	strswitch(StringByKey("BYT_OR", preamble," ",";"))	
		case "MSB":		// execute if case matches expression
			byteorder = 2
			break						// exit from switch
		case "LSB":		// execute if case matches expression
			byteorder = 3
			break
	endswitch
 
	switch(NumberByKey("BYT_NR", preamble," ",";"))	
		case 1:		// execute if case matches expression
			bytetype = 1
			numitems = numbytes
//			make /o /b /n = (numbytes) intwave
			break						// exit from switch
		case 2:		// execute if case matches expression
			bytetype = 2 
			numitems = numbytes / 2
//			make /o /i /n = (numbytes / 2) intwave
			break
	endswitch
 
	make /o /d /n = (numitems) $shortname
	wave thewave = $shortname
 
	if (stringmatch(StringByKey("BN_FMT", preamble," ",";"),"RI"))
		Fbinread /B=(byteorder) /F=(bytetype) fileref, thewave
	else
		Fbinread /B=(byteorder) /F=(bytetype) /U fileref, thewave
	endif
 
	thewave = (thewave - yoff) * ymult + yzero
 
	SetScale d, -10, 10, StringByKey("YUNIT",preamble," ",";") thewave
	SetScale /P x, (xzero - ptoff *delx), delx, StringByKey("XUNIT",preamble," ",";") thewave
//	SetScale /P x, ptoff, delx, "" thewave
 
	Note /K thewave, StringByKey("WFID",preamble," ",";")
 
	close fileref
	return 1
 
end

Back to top