-
Notifications
You must be signed in to change notification settings - Fork 21
/
WriteSegyTraceHeaderValue.m
146 lines (121 loc) · 4.75 KB
/
WriteSegyTraceHeaderValue.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
% WriteSegyTraceHeaderValue : Write trace header valaue at specific location
%
% Call:
%
% % Update all trace header values starting at position 72, in integer32
% % format, to the value 30
% data=30;
% WriteSegyTraceHeaderValue(filename,data,'pos',72,'precision','int32',);
%
% % Update all trace header values starting at position 72, in integer32
% % format, to the values in array 'data'
% ntraces=311;
% data=[1:1:311]*10;
% WriteSegyTraceHeaderValue(filename,data,'pos',72,'precision','int32');
% d_header=ReadSegyTraceHeaderValue(filename,'pos',72,'precision','int32');
%
% % Update the 'cdp' TraceHeader value:
% cdp=ReadSegyTraceHeaderValue(file,'key','cdp'); % READ CDP
% cdp=cdp+10; % change CDP
% WriteSegyTraceHeaderValue(file,cdp,'key','cdp'); % UPDATE CDP
%
% Call 'TraceHeaderDef(1)' to see a list of TraceHeader 'key' names
%
% See also ReadSegyTraceHeaderValue, PutSegyTraceHeader, TraceHeaderDef
%
function dread=WriteSegyTraceHeaderValue(filename,data,varargin);%pos,type)
pos=0;
precision='int32';
ninput=length(varargin);
% TRANSFORM VARARGING INTO PARAMETERS
cargin=1;
while (cargin<ninput)
% ENDIAN FORMAT
endian='ieee-be'; % Big Endian is default
if cargin==5, keyboard; end
if strcmp(varargin{cargin},'endian')
cargin=cargin+1;
eval(['endian_tight=varargin{cargin};'])
if endian_tight=='l',
disp(['USING LITTLE ENDIAN TYPE'])
endian='ieee-le';
else
disp(['USING BIG ENDIAN TYPE'])
end
end
if strcmp(varargin{cargin},'pos')
cargin=cargin+1;
eval(['pos=',num2str(varargin{cargin}),';']);
disp(['Reading at header postision : pos=',num2str(pos)])
end
if strcmp(varargin{cargin},'precision')
cargin=cargin+1;
eval(['precision=''',varargin{cargin},''';']);
disp(['precision : ',precision])
end
if strcmp(varargin{cargin},'key')
cargin=cargin+1;
eval(['key=''',varargin{cargin},''';']);
STH=TraceHeaderDef;
try
pos=STH.(key).pos;
precision=STH.(key).precision;
catch
disp(sprintf('Trace Header Value %s not defined',key))
pos=0; precision='int32';
hval=[];
return
end
disp(sprintf('key=%s, startpos=%d, precision=%s ',key,pos+1,precision))
end
cargin=cargin+1;
end
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
[SegyHeader]=ReadSegyHeader(filename);
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
ntraces=(DataEnd-DataStart)./(240+(SegyHeader.ns)*(BPS/8));
hval=zeros(1,SegyHeader.ntraces);
for itrace=1:SegyHeader.ntraces
if ((itrace/1000)==round(itrace/1000))
progress_txt(itrace,ntraces,'Trace #')
end
%GOTO START OF TRACE HEADER
skip=DataStart+(itrace-1)*(240+(BPS/8)*SegyHeader.ns);
fseek(segyid,skip,'bof');
fseek(segyid,pos,'cof');
if length(data)==1;
d_trace=data;
else
d_trace=data(itrace);
end
fwrite(segyid,d_trace,precision);
end
if nargout>0
dread=ReadSegyTraceHeaderValue(filename,varargin);
end
fclose(segyid);