%readtspice;
%	read tspice output data from file and assign to variables.
%	If variable "file" is set to a filename string,
%	then readtspice reads from that file; if not,
%	you will be prompted for filename;
%       example:
%	>> file='acout'; readtspice
%	* T-Spice 3.00 Simulation  Wed Aug 30 17:27:38 1995        recepac.sp  (CAZM)
%
%  	Frequency im1(photo/iphoto)       vm(rref)     vm(iphoto)       vm(rout)
%	Read 61 points in 5 vars from file acout
%	frequency=data(1,:);
%	im1photoiphoto=data(2,:);
%	vmrref=data(3,:);
%	vmiphoto=data(4,:);
%	vmrout=data(5,:);
%	>>
%	Note: will only read a single analysis per data file.
%
% 	When reading a operating point analysis, 
%  the operating points are saved as variables
%  and the entire array of operating point data is returned in 
%  op as a vector
%
%	Author: tobi 8/30/95, modified 4/9/01 for DC operating point analysis

	if exist('file')~=1,
		file=input('Filename? ','s');
	end;
	fid=fopen(file,'rt');
	
	if fid==-1,
		error('couldn''t open data file for reading');
		return;
	end;

	l=fgets(fid);
	while isempty(findstr(l,'ANALYSIS')),
		fprintf('%s',l);
		l=fgets(fid);
	end;
	if isempty(findstr(l,'DC ANALYSIS')), % if not DC analysis
		varstring=fgets(fid);
		fprintf('%s',varstring);
		[names,nvars]=sscanf(varstring,'%s ');

        % see http://www.mathworks.com/support/solutions/data/3018.shtml
        % for reading NaNs from file. (can't do it on PCWIN)
        if strcmp(computer,'PCWIN')==0,
		data=fscanf(fid,'%g',[nvars,inf]);
    else,
        data=[];
        while ~feof(fid),
            l=fgets(fid);
            if strncmp(l,'*',1), break; end; 
            if length(l)<2, continue; end;
            data=[data,sscanf(l,'%g',[nvars,1])];
        end
    end
        
		fprintf('Read %d points in %d vars from file %s\n',size(data,2),nvars,file);
		nextindex=1;
		for i=1:nvars,
			[name,count,errmsg,next]=...
				sscanf(varstring([nextindex:length(varstring)]),'%s ',1);
			nextindex=nextindex+next-1;
			m=1; newname='                               ';
			for j=1:length(name),
				if isletter(name(j)) | (name(j)>='0' & name(j)<='9'),
					newname(m)=lower(name(j));
					m=m+1;
				end;
	%			if name(j)=='(',
	%				name(j)=' ';
	%			elseif name(j)==')',
	%				name(j)=' ';
	%			elseif name(j)==',',
	%				name(j)=' ';
	%			elseif name(j)=='/',
	%				name(j)=' ';
	%			end;
			end;
			name=deblank(newname);
			cmd=sprintf('%s=data(%d,:);',name,i);
			fprintf('%s\n',cmd);

			eval(cmd);	
		end;
	else, % come here for operating point analysis
		op=[];
		
		l=fgetl(fid); % get line w/o terminator
		% parse out variable name and value
		while ~isempty(l),
			[name,rest]=strtok(l);
			m=1; newname='                                ';
			for j=1:length(name),
				if isletter(name(j)) | (name(j)>='0' & name(j)<='9'),
					newname(m)=lower(name(j));
					m=m+1;
				end;
	%			if name(j)=='(',
	%				name(j)=' ';
	%			elseif name(j)==')',
	%				name(j)=' ';
	%			elseif name(j)==',',
	%				name(j)=' ';
	%			elseif name(j)=='/',
	%				name(j)=' ';
	%			end;
			end;
			name=deblank(newname);
			[equals,rest]=strtok(rest);
			[val,count,errmsg,nextindex]=sscanf(rest,'%g');
			op=[op,val];
			cmd=sprintf('%s=val;',name);
			fprintf('%s\n',cmd);
			eval(cmd);
			l=fgetl(fid); % get line w/o terminator
		end
		fprintf('.op results stored in vector ''op''\n');
	end
fclose(fid);
clear cmd count equals errmsg j l m name newname nextindex rest val
return;

