function [seis,hdr]=segy2mat(filename,bite_order,delay,window); %function [seis,hdr]=segy2mat(filename); % * * * * %Reads PASSCAL SEGY in file FILENAME as generated by PASSCAL 1.9 ref2segy %SEIS is the seismic trace and HDR is a structure containing trace header (only some %elements are returned in HDR but this is easily changed) % %Note that PASSCAL SEGY is non-standard. It has not reel header, and has only 1 %trace per file, and the trace header is non standard % %If a second argument is passed to the function,file will be opened with %little-endian bite order %----Check inputs & GLOBALS hdr=[]; seis=[]; if nargin<1 error('READPSEGY needs at least 1 input argument') end %----Check for the existence of the file and get its length %string = [ 'ls -l ', filename, ' | awk ''{print $5}''']; %[status, ans]=unix(string); string = dir(filename); if length(filename)>21; cmp_filename = filename(length(filename)-21:length(filename)); else cmp_filename = filename; end %if strcmp(char(string.name),cmp_filename) == 0 if exist(filename)~=2 % disp([' Problems finding PASSCAL SEGY file ',filename]); % disp(' ') return end %----Open file if nargin>1 & bite_order(1)=='l' fid=fopen(filename,'r','ieee-le'); elseif nargin>1 & bite_order(1)=='b' fid=fopen(filename,'r','ieee-be'); else fid=fopen(filename,'r'); end if fid<0 disp([' Failed to open PASSCAL SEGY file ',filename]); disp(' ') return end %----Read trace header [in,count(1)]=fread(fid,7,'int32'); if isempty(in); disp([' File unreadable for some reason?!?']) return end [in2,count(2)]=fread(fid,4,'int16'); [in3,count(3)]=fread(fid,8,'int32'); [in4,count(4)]=fread(fid,2,'int16'); [in5,count(5)]=fread(fid,4,'int32'); [in6,count(6)]=fread(fid,46,'int16'); [c7,count(7)]=fread(fid,18,'char'); in7=[0; 0; 0]; [in8,count(8)]=fread(fid,1,'int16'); [in9,count(9)]=fread(fid,1,'int32'); [in10,count(10)]=fread(fid,8,'int16'); [in11,count(11)]=fread(fid,1,'float32'); [in12,count(12)]=fread(fid,2,'int16'); [in13,count(13)]=fread(fid,3,'int32'); in=[in; in2; in3; in4; in5; in6; in7; in8; in9; in10; in11; in12; in13]; if sum(count)~=105 disp([' Failed to read header - Error code ',int2str(sum(count))]); disp(' ') fclose(fid); return end %----Assign header values (add your own favorites) hdr.year=in(60); hdr.day=in(61); hdr.hour=in(62); hdr.min=in(63); hdr.sec=in(64); hdr.msec=in(78); %hdr.time_basis=in(65); %keyboard if in(39)~=32767 hdr.nsamp=in(39); else hdr.nsamp=in(88); %hdr.nsamp = 360000; % this is a temporary stopgap measure for Jonathan's data end hdr.nsamp=in(88); hdr.sps=1e6/in(76); hdr.channel=in(4); hdr.gain = in(42); %hdr.format=in(77); %hdr.trigyear=in(79); %hdr.trigday=in(80); %hdr.trighour=in(81); %hdr.trigmin=in(82); %hdr.trigsec=in(83); %hdr.trigmsec=in(84); hdr.atod=in(85); %hdr.inst=in(86); %hdr.bigtime=date2secnds(in(60),in(61),in(62),in(63),in(64)+in(78)/1000); %hdr.bigtrigtime=date2secnds(in(79),in(80),in(81),in(82),in(83)+in(84)/1000); format=in(77); if nargin>1 & length(bite_order)>1; % first character indicates bite_order, more than one character indicates just return hdr return end if format==0 byte_type='int16'; bt=2; else byte_type='int32'; bt=4; end if nargin>2 delay_samples = round(delay*hdr.sps)*bt; else delay=0; delay_samples = 0; end fseek(fid,delay_samples,'cof'); if nargin>3 window_samples = floor(window*hdr.sps); else window_samples = hdr.nsamp; end [seis,count]=fread(fid,window_samples,byte_type); if delay_samples*hdr.sps + window_samples*hdr.sps > hdr.nsamp %disp([' Window extends beyond this file ', filename]); end if delay~=0 secs=double(jhms2secs(hdr.day,hdr.hour,hdr.min,hdr.sec,hdr.msec)+delay); [hdr.day,hdr.hour,hdr.min,hdr.sec,hdr.msec]=secs2jhms(secs); hdr.nsamp = count; end fclose(fid); hdr.range=max(seis)-min(seis); if count~=hdr.nsamp %disp([' Failed to read all data for file ',filename]); disp(' ') return end %keyboard %[seis,hdr.reduction]=normalize(seis);