%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%%% Read waveform data from Tektronix Oscilloscope WFM format file
%%% 
%%% Obata, Toshiaki
%%% ADSC/TEK Japan
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Read Waveform Data From Tek WFM File
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function	[data, taxis] = ReadTekWFMFile(filePath, varargin)

	data = [];
    sI = 0.0;
    xZ = 0.0;
    len = 0;
    extraPoints = 0;
    
    if nargin == 2
        ex = varargin(1);
        if strcmp(ex, 'on') == 1
            extraPoints = 1;
        end
    elseif nargin ~= 1
        disp('Syntax Error: [data, len, sampleInterval, xZero] = ReadTekWFMFile(filePath, [extraPoints])');
		return;
    end
    
	f = fopen(filePath, 'r');
	if f < 0
		disp(['Error: Can not open ', filePath, '.']);
		return;
    end

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%% Waveform Static File Information
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	byteOrderVerification = fread(f, 1, 'ushort');
	if byteOrderVerification == 3855 
        endianType = 'ieee-le';
    elseif byteOrderVerification == 61680
		endianType = 'ieee-be';
    else
        disp(['Error: Can not open ',filePath,'.']);
        fclose(f);
		return;
	end

	fclose(f);
	f = fopen(filePath, 'r', endianType);

	byteOrderVerification = fread(f, 1, 'ushort');
	versionNumber =	fread(f, 8, 'uint8');
    versionNumberChar = char(versionNumber);
    version = sscanf(versionNumberChar, ':WFM#%d');
	numDigitInByteCount = 	fread(f, 1, 'int8');
	numberOfBytesToEOF = fread(f, 1, 'long');
	numberOfBytesPerPoint = fread(f, 1, 'int8');
    if numberOfBytesPerPoint ~= 1 & numberOfBytesPerPoint ~= 2
        disp('Error: Invalid bytes per point [ Can read 1 byte or 2 bytes per point ].');
        fclose(f);
		return;
    end
        
	bytesOffsetToBegginingOfCurveBuffer = fread(f, 1, 'long');
	skipInfo = fread(f, 52, 'uint8');
	n = fread(f, 1, 'ulong');
	sizeOfWaveformHeader = fread(f, 1, 'ushort');
    
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%% Waveform Header
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    setType = fread(f, 1, 'int');
%     if setType ~= 0
%         disp('Error: Can not read FastFrame data file.');
%         fclose(f);
% 		return;
%     end
    
    wfmCount = fread(f, 1, 'ulong');
	skipInfo = fread(f, 36, 'uint8');
    
	dataType =fread(f, 1, 'int');
    if dataType ~= 2
        disp('Error: Can read only WFMDATA VECTOR file.');
        fclose(f);
		return;
    end
    
	if version > 1
		skipInfo = fread(f, 42, 'uint8');
	else
		skipInfo = fread(f, 40, 'uint8');
    end
    
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%% Explicit Dimension 1
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    dimScale = fread(f, 1, 'double');
	dimOffset = fread(f, 1, 'double');
	skipInfo = fread(f, 56, 'uint8');
	explicitFormat = fread(f, 1, 'long');
	storageType = fread(f, 1, 'long');
    if storageType ~= 0
        disp('Error: Can read only SAMPLE data file.');
        fclose(f);
		return;
    end
    
	skipInfo = fread(f, 20, 'uint8');
	if version > 2
		skipInfo = fread(f, 60, 'uint8');
	else
		skipInfo = fread(f, 56, 'uint8');
	end

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%% Explicit Dimension 2
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	if version > 2
		skipInfo = fread(f, 160, 'uint8');
	else
		skipInfo = fread(f, 156, 'uint8');
    end

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%% Inplicit Dimension 2
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%1
	horizontalScale = fread(f, 1, 'double');
    horizontalOffset = fread(f, 1, 'double');
    skipInfo = fread(f, 116, 'uint8');

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%% Inplicit Dimension 2
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    skipInfo = fread(f, 132, 'uint8');

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%% Time Base 1
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	realPointSpacing = fread(f, 1, 'ulong');
	sweep = fread(f, 1, 'int');
	typeOfBase = fread(f, 1, 'int');
    if typeOfBase ~= 0
        disp('Error: Can read only BASE TIME data file.');
        fclose(f);
		return;
    end

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%% Time Base 2
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	skipInfo = fread(f, 12, 'uint8');

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%% FastFrame Frames
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%	skipInfo = fread(f, 54, 'uint8'); % skip 24 bytes and 30 bytes 

    IdimOffset=zeros(5000,30);
    Itimestamp=zeros(5000,24);

%     for k=1:5000
%         
% %      Itimestamp = fread(f, 24, 'double');
% %      IdimOffset = fread(f, 30, 'double');
%       Itimestamp(k,:) = fread(f, 24, 'uint8');
%       IdimOffset(k,:) = fread(f, 30, 'uint8');
% 
% 
%     end

    
   skipInfo = fread(f, 269992, 'uint8'); % skip 24 bytes and 30 bytes 

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	%%% Read Curve Data
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	byteFileSize = numberOfBytesToEOF + 11;
	byteCurveSize = byteFileSize - bytesOffsetToBegginingOfCurveBuffer - 8;
	realCurveSize = byteCurveSize / numberOfBytesPerPoint;
    
    if extraPoints == 1
        curveSize = realCurveSize
        skipPoints = 0
    else
        curveSize = 100 * floor(realCurveSize / 100);
        skipPoints = floor(( realCurveSize - curveSize ) / 2);
    end
    
    if numberOfBytesPerPoint == 1
        if skipPoints > 0
            skipInfo = fread(f, skipPoints, 'int8');
        end 
		data = fread(f, curveSize, 'int8');
	elseif numberOfBytesPerPoint == 2
        if skipPoints > 0
            skipInfo = fread(f, skipPoints, 'short');
        end 
		data = fread(f, curveSize, 'short');
	end
	fclose(f);

	longdata = data' * dimScale + dimOffset;
       
    data=zeros(1000,(n+1));
    
    wavecount=1;
    pcount=1;
    
    for i=1:(n)
              
        data(:,i)=longdata((((i-1)*1032)+1):(((i-1)*1032)+1000));
    end
    
	len = curveSize;
	sI = horizontalScale;
    xZ = horizontalOffset;

    taxis=zeros(1000,1);
    
    for k=1:1000
        taxis(k,1)= xZ + sI*k;
    end
    
        
return;
