-
Notifications
You must be signed in to change notification settings - Fork 21
/
ReadSegyFast.m
239 lines (194 loc) · 7.58 KB
/
ReadSegyFast.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
% ReadSegyFast : Reads a SEG Y rev 1 formatted file, without header values (faster than ReadSegy)
%
% Call :
% [Data]=ReadSegyFast(filename);
% and equivalent to :
% [Data]=ReadSegy(filename);
%
%
% Read only the data of a SegFile - NOT Their headers.
% Much faster than ReadSegy
%
% 'minmax', 'skip'
%
% Implemented using the syntax of the SEG-Y revised format :
% SEGY-Y rev 0, SEG-Y rev 1 as described in
% http://seg.org/publications/tech-stand/
%
% Extended Textual Header is not yet tested
% If you would like it implemented, please send me an SEGY file with
% that sort of information, as well as a description of the segy file
%
%
% (C) 2001, Thomas Mejer Hansen, [email protected]
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
%
%
%
%
function [Data,SegyHeader,SegyTraceHeadersBinary]=ReadSegyFast(filename,varargin);
mfilename='ReadSegyFast';
if nargin==0,
SegymatVerbose([mfilename,' : ', filename,' does not exist !'])
Data=[];SegyTraceHeaders=[];SegyHeader=[];HeaderInfo=[];
return
end
% Initialize varargin values
dsf=[];
revision=[];
endian_tight=[];
tmin=[];tmax=[];
headermin=[];headermax=[];header=[];
jump=[];
SegyHeader=[];
segyid = fopen(filename,'r','b'); % ALL DISK FILES ARE IN BIG
% ENDIAN FORMAT, ACCORDING TO SEG
% Y rev 1
fseek(segyid,0,'eof');
BytesInFile = ftell(segyid);
fseek(segyid,0,'bof');
ninput=nargin;
% NEXT TWO LINES TO ENUSRE THAT VARARGIN CAN BE PASSED TO FUNCTION
if ninput==2
% CALL USING VARARGIN
ninput=1+length(varargin{1});
varargin=varargin{1};
else
% DIRECT CALL
ninput=length(varargin);
end
% TRANSFORM VARARGING INTO PARAMETERS
cargin=1;
while (cargin<ninput)
if strcmp(varargin{cargin},'jump')
cargin=cargin+1;
eval(['jump=',num2str(varargin{cargin}),';']);
SegymatVerbose(['JUMP : Read only every ',num2str(jump),'th trace'])
end
if strcmp(varargin{cargin},'minmax')
cargin=cargin+1;
eval(['header=''',varargin{cargin},''';']);
cargin=cargin+1;
eval(['headermin=',num2str(varargin{cargin}),';']);
cargin=cargin+1;
eval(['headermax=',num2str(varargin{cargin}),';']);
SegymatVerbose(['MIN MAX : Using header ',header,' from ',num2str(headermin),' to ',num2str(headermax)])
end
if strcmp(varargin{cargin},'trange')
cargin=cargin+1;
eval(['tmin=',num2str(varargin{cargin}),';']);
cargin=cargin+1;
eval(['tmax=',num2str(varargin{cargin}),';']);
SegymatVerbose(['TRANGE : tmin=',num2str(tmin),' tmax=',num2str(tmax)])
end
if strcmp(varargin{cargin},'revision')
cargin=cargin+1;
eval(['revision=',num2str(varargin{cargin}),';']);
SegymatVerbose(['USING REVISION : rev=',num2str(revision)])
end
if strcmp(varargin{cargin},'dsf')
cargin=cargin+1;
eval(['dsf=',num2str(varargin{cargin}),';']);
SegymatVerbose(['USING Data Sample Format : dsf=',num2str(dsf)])
end
if strcmp(varargin{cargin},'SegyHeader')
cargin=cargin+1;
SegyHeader=varargin{cargin};
SegymatVerbose(['USING LOADED SEGYHEADER'])
end
cargin=cargin+1;
end
% SKIP DATA SEEMS OUTDATED, COMMENTING OUT 2003-10-24
%if exist('SkipData')==0,
% SkipData=0; % [0] READ ONLY HEADER VALUES, [1] READ IN ALL DATA
%end%%
%
%if SkipData==1, SegymatVerbose(['Not reading data - headers only']), end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BINARY HEADERS
if isempty(SegyHeader)==1,
SegyHeader=GetSegyHeader(segyid);
else
SegymatVerbose([mfilename,' - Using supplied SegyHeader'])
end
% APPLY CHANGES TO SEGY HEADER IF NEEDE
if isempty(revision)==0,
SegyHeader.SegyFormatRevisionNumber=revision;
SegymatVerbose([mfilename,' - Manually set SEG-Y revision to ',num2str(revision)])
end
if isempty(dsf)==0,
SegyHeader.DataSampleFormat=dsf;
end
% JUST SOME INFORMATION TO WRITE TO SCREEN :
Revision=SegyHeader.SegyFormatRevisionNumber;
if Revision>0, Revision=1; end
FormatName=SegyHeader.Rev(Revision+1).DataSampleFormat(SegyHeader.DataSampleFormat).name;
Format=SegyHeader.Rev(Revision+1).DataSampleFormat(SegyHeader.DataSampleFormat).format;
BPS=SegyHeader.Rev(Revision+1).DataSampleFormat(SegyHeader.DataSampleFormat).bps;
txt=['SegyRevision ',sprintf('%0.4g',Revision),', ',FormatName,'(',num2str(SegyHeader.DataSampleFormat),')'];
ns=SegyHeader.ns;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% READ TEXTURAL FILE HEADER EXTENSION IF NEEDED
%if SegyHeader.NumberOfExtTextualHeaders~=0
% SegymatVerbose(['---------------------------------------------------'])
% SegymatVerbose(['extendeD textual file headers are not supported yet'])
% SegymatVerbose(['They are simply skipped'])
% SegymatVerbose(['---------------------------------------------------'])
%end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% READ DATA
%Segy=fread(segyid,4000,'float32');
fseek(segyid,0,'eof'); DataEnd=ftell(segyid);
DataStart=3600+3200*SegyHeader.NumberOfExtTextualHeaders;
fseek(segyid,DataStart,'bof'); % Go to the beginning of the file
ntraces=(DataEnd-DataStart)./(240+(SegyHeader.ns)*(BPS/8));
SegymatVerbose(['Number of Samples Per Trace=',num2str(SegyHeader.ns)])
SegymatVerbose(['Number of Traces=',num2str(ntraces)])
% Set Format :
Revision=SegyHeader.SegyFormatRevisionNumber;
if Revision>0, Revision=1; end
Format=SegyHeader.Rev(Revision+1).DataSampleFormat(SegyHeader.DataSampleFormat).format;
bitpersample=SegyHeader.Rev(Revision+1).DataSampleFormat(SegyHeader.DataSampleFormat).bps;
bytepersample=bitpersample/8;
ntraceheader=240/bytepersample;
Data=fread(segyid,[ns+ntraceheader ntraces],Format);
SegyTraceHeadersBinary=Data(1:ntraceheader,:);
Data=Data([ntraceheader+1:1:ns+ntraceheader],:);
%SegymatVerbose(['Now=',num2str(ftell(segyid)),' END=',num2str(DataEnd)])
% THE FOLLOWING DOES NOT WORK FOR OCTAVE (uint32)
SegyHeader.DataFormat=Format;
if (strcmp(Format,'uint32')==1), % IBM FLOATING POINT
% CONVERT FROM FLOATING POINT
verbose=2;
SegymatVerbose([mfilename,'Converting from IBM, DataFormat :',SegyHeader.DataFormat],verbose);
Data=ibm2num(uint32(Data));
end;
existJump=1-isempty(jump);
existHeader=1-isempty(header);
existTmin=1-isempty(tmin);
existTmax=1-isempty(tmax);
if existJump
usetraces=[jump:jump:ntraces];
ntraces=length(usetraces);
Data=Data(:,usetraces);
end
if (existTmin==1)&(existTmax==1)
SegymatVerbose('TRANGE')
% NEXT LINE SHOULD CONSIDER THAT ns in Trace and Segy Header could vary !!!
origtrange=[1:1:SegyHeader.ns].*SegyHeader.dt.*1e-6;
gooddata=find(origtrange>tmin & origtrange<tmax);
Data=Data(gooddata,:);
end