function [data,header,kstnm] = sac2mata(sacfile,start,duration);
%sac2mata  loads a SAC2000 file into Matlab, along with header information.
%  [data,header,kstnm] = sac2mata(sacfile, start, duration);
%
%  sacfile must be a STRING, eg.  [fff,hhh,name]=sac2mata('dir1/file1.sac')
%  sac2mata also takes two optional arguments:
%    start - INTEGER - sets the starting offset for data, this defaults to 0
%    duration - sets the duration for the data, this defaults to 'eof'
%     if duration='eof', then sac2mata reads to end of file
%     if duration is an INTEGER (start+duration < length of sac file)
%       then sac2mata reads from start to start+duration
%  eg. [fff,hhh,name]=sac2mata('dir1/file1.sac',31415,1000);
%    ...this will read in 1000 points, starting at point 31415, as long as the
%    file to be read is less than 31415+1000=32425 points long.
%
%Output:
%data=	the data from the file
%header=the first 105 header variables, as described on the SAC Home Page
%	http://www.llnl.gov/sac/
%	http://www.llnl.gov/sac/SAC_Manuals/FileFormatPt1.html
%kstnm=	KSTNM, the station name (string)
%	(header and kstnm are optional)
%
%Some common/useful headers:
%header(1:3) DELTA,DEPMIN,DEPMAX
%header(57) DEPMEN
%header(6:7) B,E
%header(8) Event origin time (seconds relative to reference time)
%header(32:39) STLA,STLO,STEL,STDP,EVLA,EVLO,EVEL,EVDP
%header(51:54) DIST,AZ,BAZ,GCARC        
%header(71:76)  NZYEAR,NZDAY,NZHOUR,NZMIN,NZSEC,NZMSEC
%header(80) NPTS
%						Case Bradford, 8 June 2004
sacfid = fopen(sacfile,'r');
if sacfid==-1;error='Not a valid path.'
return;end

%The first 70 header variables are floating point
header1 = fread(sacfid,70,'float32');

%The next 35 are integers (after that they're a mix of strings)
header2 = fread(sacfid,35,'int32');

%Outputs the number of points being read to the screen
header=[header1;header2];
npts = header(80);
total_npts=npts

%Now check the duration/start values for logic:
if nargin==1;start=0;duration='eof';end;
if nargin==2;
if start > npts;error='Start point is after end of file.'
return;end
if start < 0;error='Start time cannot be negative.'
return;end
if isnan(start);error='Start time cannot be NaN.'
return;end
duration='eof';end;
if nargin==3;
if start > npts;error='Start point is after end of file.'
return;end
if start < 0;error='Start time cannot be negative.'
return;end
if isempty(start);
  start=0;
end
	if ischar(duration);
		if not(strcmp(duration,'eof'));
		  error = 'Duration is an undefined string.'
                return;
                end
	elseif duration > (npts-start);
	  error='Duration is past end-of-file.'
	return;
	elseif duration <= 0;
	  error='Duration cannot be negative, or zero.'
	return;
	elseif isempty(duration);
	  duration='eof';
	end
end

if duration=='eof';npts = npts-start;else;npts=duration;end;
npts=round(npts);
start=round(start);
number_read=npts
starting_at=start

%kstnm, the station name, is a string stored in the 110th header variable
%kstnm = KSTNM (First 3 letters of the station name)
fseek(sacfid,110*4,-1);
kstnm = char(fread(sacfid,3)');

%Now read the data...
fseek(sacfid,158*4+start*4,-1);
data(1:npts) = fread(sacfid,npts,'float32');
status = fclose(sacfid);
return

