forked from cultpenguin/segymat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadSegyHeader.m
171 lines (136 loc) · 5.7 KB
/
ReadSegyHeader.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
% ReadSegyHeader : Reads a SEG Y Binary Header
%
% Call :
% [SegyHeader]=ReadSegyHeader(filename);
%
% To read using little endian :
% [SegyHeader]=ReadSegyHeader(filename,'endian','l');
%
% (C) 2001-2004, Thomas Mejer Hansen, [email protected]/[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 [SegyHeader]=ReadSegyHeader(filename,varargin);
if ~(exist(filename)==2'),
SegymatVerbose([mfilename,' : ', filename,' does not exist !'])
Data=[];SegyTraceHeaders=[];SegyHeader=[];HeaderInfo=[];
return
end
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
% ENDIAN FORMAT
endian='ieee-be'; % Big Endian is default
if strcmp(varargin{cargin},'endian')
cargin=cargin+1;
eval(['endian_tight=varargin{cargin};'])
if endian_tight=='l',
SegymatVerbose(['USING LITTLE ENDIAN TYPE'])
endian='ieee-le';
else
SegymatVerbose(['USING BIG ENDIAN TYPE'])
end
end
cargin=cargin+1;
end
if exist('SkipData','var')==0,
SkipData=0; % [0] READ ONLY HEADER VALUES, [1] READ IN ALL DATA
end
if SkipData==1, SegymatVerbose(['Not reading data - headers only']), end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TRY TO ESTIMATE NUMBER OF TRACES
if exist('endian')==1,
segyid = fopen(filename,'r',endian);
else
segyid = fopen(filename,'r','ieee-be'); % ALL DISK FILES ARE IN BIG
end % ENDIAN FORMAT, ACCORDING TO
% SEGY Y rev 1
SegyHeader=GetSegyHeader(segyid);
try
Revision=SegyHeader.SegyFormatRevisionNumber;
if Revision>0, Revision=1; end
if (SegyHeader.DataSampleFormat>length(SegyHeader.Rev(Revision+1).DataSampleFormat));
SegymatVerbose([mfilename,' : WARNING : YOU HAVE SELECTED (OR THE FILE IS FORMATTED SUCH THAT) A DATASAMPLE FORMAT THAT IS NOT DEFINED. \nREMEBER IEEE IS NOT SPECIFIED IN THE SEGY REV0 STANDARD !'])
if (Revision==0)
SegymatVerbose([mfilename,' : TRYING TO USE REVISION 1 AS OPPOSED TO REVISION 0'])
Revision=1;
if (SegyHeader.DataSampleFormat>length(SegyHeader.Rev(Revision+1).DataSampleFormat));
SegymatVerbose([mfilename,' : FATAL ERROR : STILL THE DATASAMPLE FORMAT IS NOT SUPPRTED - EXITING (Report error to [email protected])'])
else
SegymatVerbose([mfilename,' : APPARENT SUCCES CHANING FROM Revision 0 to 1 - Continuing'])
SegyHeader.SegyFormatRevisionNumber=1; % FORCING REVISION TO BE 1 !!!
end
end
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),')'];
fseek(segyid,0,'eof'); DataEnd=ftell(segyid);
DataStart=3600+3200*SegyHeader.NumberOfExtTextualHeaders;
fseek(segyid,DataStart,'bof'); % Go to the beginning of the file
fseek(segyid,DataStart,'bof'); % Go to the beginning of the file
SegyHeader.ntraces=(DataEnd-DataStart)./(240+(SegyHeader.ns)*(BPS/8));
catch
% could not estimate ntraces
end
fclose(segyid);