1 Read in and Write out SAC Format Data

Matlab is very powerful for data processing and some seismologists utilize it to processing seismic data. Here we show you how to read in and write out a SAC format file using Matlab.

1.1 Read in SAC

  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
% ----------------------------------------------------------------------- %
% READ SAC FILE: HEADER VARIABLES AND DATA.                               %
% ----------------------------------------------------------------------- %


function [ sachead, data ] = readSAC( fname )

%% Check parameters.
    if nargin < 1
        fprintf( 'Usage: [ sachead, data ] = readSAC( fname )\n' );
        sachead = [];
        data = [];
        return;
    end
	fid = fopen( fname, 'rb' );
	if fid == -1
		fprintf(strcat('Error opening SAC file: %s.\n', fname));
        sachead = [];
        data = [];
		return;
    end
    
%% --------------- Read in  70 float header variables ------------------- %
%------------------------------ read 70  float -------------------------- %
	tt = fread( fid, 70, 'single' );
	sachead.delta=tt(1)	;		    % RF time increment, sec    %
	sachead.depmin=tt(2);			%    minimum amplitude      %
	sachead.depmax=tt(3);			%    maximum amplitude      %
	sachead.scale=tt(4);			%    amplitude scale factor %
	sachead.odelta=tt(5);		    %    observed time inc      %
	sachead.b=tt(6);				% RD initial time - wrt nz* %
	sachead.e=tt(7);				% RD end time               %
	sachead.o=tt(8);				%    event start            %
	sachead.a=tt(9);				%    1st arrival time       %
	sachead.internal1=tt(10);		%    internal use           %
	sachead.t0=tt(11);				%    user-defined time pick %
	sachead.t1=tt(12);				%    user-defined time pick %
	sachead.t2=tt(13);				%    user-defined time pick %
	sachead.t3=tt(14);			    %    user-defined time pick %
	sachead.t4=tt(15);				%    user-defined time pick %
	sachead.t5=tt(16);				%    user-defined time pick %
	sachead.t6=tt(17);				%    user-defined time pick %
	sachead.t7=tt(18);				%    user-defined time pick %
	sachead.t8=tt(19);				%    user-defined time pick %
	sachead.t9=tt(20);				%    user-defined time pick %
	sachead.f=tt(21);				%    event end, sec > 0     %
	sachead.resp0=tt(22);			%    instrument respnse parm %
	sachead.resp1=tt(23);			%    instrument respnse parm %
	sachead.resp2=tt(24);			%    instrument respnse parm %
	sachead.resp3=tt(25);			%    instrument respnse parm %
	sachead.resp4=tt(26);			%    instrument respnse parm %
	sachead.resp5=tt(27);			%    instrument respnse parm %
	sachead.resp6=tt(28);			%    instrument respnse parm %
	sachead.resp7=tt(29);			%    instrument respnse parm %
	sachead.resp8=tt(30);			%    instrument respnse parm %
	sachead.resp9=tt(31);			%    instrument respnse parm %
	sachead.stla=tt(32)	;			%  T station latititude      %
	sachead.stlo=tt(33)	;			%  T station longitude       %
	sachead.stel=tt(34)	;			%  T station elevation, m    %
	sachead.stdp=tt(35)	;			%  T station depth, m        %
	sachead.evla=tt(36)	;			%    event latitude          %
	sachead.evlo=tt(37)	;			%    event longitude         %
	sachead.evel=tt(38)	;			%    event elevation         %
	sachead.evdp=tt(39)	;			%    event depth             %
	sachead.mag=tt(40)	;	        %    reserved for future use %
	sachead.user0=tt(41);			%    available to user       %
	sachead.user1=tt(42);			%    available to user       %
	sachead.user2=tt(43);			%    available to user       %
	sachead.user3=tt(44);			%    available to user       %
	sachead.user4=tt(45);			%    available to user       %
	sachead.user5=tt(46);			%    available to user       %
	sachead.user6=tt(47);			%    available to user       %
	sachead.user7=tt(48);			%    available to user       %
	sachead.user8=tt(49);			%    available to user       %
	sachead.user9=tt(50);			%    available to user       %
	sachead.dist=tt(51)	;		    %    stn-event distance, km  %
	sachead.az=tt(52)	;		    %    event-stn azimuth       %
	sachead.baz=tt(53)	;		    %    stn-event azimuth       %
	sachead.gcarc=tt(54);			%    stn-event dist, degrees %
	sachead.internal2=tt(55);		%    internal use            %
	sachead.internal3=tt(56);		%    internal use            %
	sachead.depmen=tt(57)	;		%    mean value, amplitude   %
	sachead.cmpaz=tt(58)	;		%    component azimuth       %
	sachead.cmpinc=tt(59)	;		%    component inclination   %
	sachead.xminimum=tt(60)	;	    %    Minimum value of X (Spectral files only)	%
	sachead.xmaximum=tt(61)	;	    %    Maximum value of X (Spectral files only)	%
	sachead.yminimum=tt(62)	;	    %    Minimum value of Y (Spectral files only)	%
	sachead.ymaximum=tt(63)	;	    %    Maximum value of Y (Spectral files only)	%
	sachead.unused6=tt(64)	;	    %    reserved for future use %
	sachead.unused7=tt(65)	;	    %    reserved for future use %
	sachead.unused8=tt(66)	;	    %    reserved for future use %
	sachead.unused9=tt(67)	;	    %    reserved for future use %
	sachead.unused10=tt(68)	;	    %    reserved for future use %
	sachead.unused11=tt(69)	;	    %    reserved for future use %
    sachead.unused12=tt(70) ;       %    reserved for future use %
 
%% ---------------- Read in 40 integer header variables ----------------- %
% --------------------------- read 40 integers -------------------------- %	
	tt = fread( fid, 40, 'int32' );
 
	sachead.nzyear=tt(1);           %    Reference year          %
	sachead.nzjday=tt(2);           %    Reference julian day    %
	sachead.nzhour=tt(3);           %    Reference hour          %
 	sachead.nzmin=tt(4);            %    Reference minite        %
	sachead.nzsec=tt(5);            %    Reference second        %
	sachead.nzmsec=tt(6);           %    Reference mili-second   %
	sachead.nvhdr=tt(7);
	sachead.norid=tt(8);
	sachead.nevid=tt(9);
	sachead.npts=tt(10);
	sachead.internal7=tt(11);
	sachead.nwfid=tt(12);
	sachead.nxsize=tt(13);
	sachead.nysize=tt(14);
	sachead.unused15=tt(15);
	sachead.iftype=tt(16);
	sachead.idep=tt(17);
	sachead.iztype=tt(18);
	sachead.unused16=tt(19);
	sachead.iinst=tt(20);
	sachead.istreg=tt(21);
	sachead.ievreg=tt(22);
	sachead.ievtyp=tt(23);
	sachead.iqual=tt(24);
	sachead.isynth=tt(25);
	sachead.imagtyp=tt(26);
	sachead.imagsrc=tt(27);
	sachead.unused19=tt(28);
	sachead.unused20=tt(29);
	sachead.unused21=tt(30);
	sachead.unused22=tt(31);
	sachead.unused23=tt(32);
	sachead.unused24=tt(33);
	sachead.unused25=tt(34);
	sachead.unused26=tt(35);
	sachead.leven=tt(36);
	sachead.lpspol=tt(37);
	sachead.lovrok=tt(38);
	sachead.lcalda=tt(39);
	sachead.unused27=tt(40);

%% -------------- Read in 23 character header variables ---------- %
% ------------------------ read 192 characters -------------------------- %
	xx = fread( fid, 192, '*char');
	sachead.kstnm=xx(1:8);		    %    station name           %
	sachead.kevnm=xx(9:24);		    %    event name             %
	sachead.khole=xx(25:32);		%    man-made event name    %
	sachead.ko=xx(33:40)	;		%    event origin time id   %
	sachead.ka=xx(41:48)	;		%    1st arrival time ident %
	sachead.kt0=xx(49:56)	;		%    time pick 0 ident      %
	sachead.kt1=xx(57:64)	;		%    time pick 1 ident      %
	sachead.kt2=xx(65:72)	;		%    time pick 2 ident      %
	sachead.kt3=xx(73:80)	;		%    time pick 3 ident      %
	sachead.kt4=xx(81:88)	;		%    time pick 4 ident      %
	sachead.kt5=xx(89:96)	;	    %    time pick 5 ident      %
	sachead.kt6=xx(97:104)	;	    %    time pick 6 ident      %
	sachead.kt7=xx(105:112)	;		%    time pick 7 ident      %
	sachead.kt8=xx(113:120)	;		%    time pick 8 ident      %
	sachead.kt9=xx(121:128)	;		%    time pick 9 ident      %
	sachead.kf=xx(129:136)	;		%    end of event ident     %
	sachead.kuser0=xx(137:144);		%    available to user      %
	sachead.kuser1=xx(145:152);		%    available to user      %
	sachead.kuser2=xx(153:160);		%    available to user      %
	sachead.kcmpnm=xx(161:168);		%    component name         %
	sachead.knetwk=xx(169:176);		%    network name           %
	sachead.kdatrd=xx(177:184);		%    date data read         %
	sachead.kinst=xx(185:192)	;	%    instrument name        %	

%% ---------------------------------------------------------------------- %
% --------------------------- read data---------------------------------- %
	data = fread( fid, sachead.npts, 'single' );

% ----------------------------------------------------------------------- %
% ------------------------- close file ---------------------------------- %
fclose( fid );

end

1.2 Write out SAC

  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
% ----------------------------------------------------------------------- %
% Write SAC format data.                                                  %
% Usage: writeSAC( SACFileName, sachead, data);                           %
%        SACFileName: File name written;                                  %
%        sachead: Header variables struct of SAC;                         %
%        data: Single 1-D amplitude vector of SAC.                        % 
% ----------------------------------------------------------------------- %


function writeSAC( SACName, sachead, data )

%% Check parameters.
    if nargin < 3
        fprintf( 'Usage: writeAllSAC( SACName, sachead, data )\n' );
        return;
    end

% Open binary file to be written.
fid = fopen( SACName, 'wb' );

% Check opening file.
if fid == -1
    fprintf( 'Error writing SAC file: %s.\n', SACName );
    return;
end

%% ------------- Write out 70 float header variables -------------------- %
%------------------------------ write 70 float -------------------------- %
    tt = zeros(1, 70, 'single');
	tt(1)  = sachead.delta	;	        %    time increment, sec    %
	tt(2)  = sachead.depmin;		    %    minimum amplitude      %
	tt(3)  = sachead.depmax;		    %    maximum amplitude      %
	tt(4)  = sachead.scale;			    %    amplitude scale factor %
	tt(5)  = sachead.odelta;		    %    observed time inc      %
	tt(6)  = sachead.b;				    %    initial time - wrt nz* %
	tt(7)  = 	sachead.e; 			    %    end time               %
    tt(8)  = 	sachead.o; 			    %    event start            %
    tt(9)  = 	sachead.a ;			    %    1st arrival time       %
    tt(10) = 	sachead.internal1; 	     %    internal use           %
    tt(11) = 	sachead.t0;			    %    user-defined time pick %
    tt(12) = 	sachead.t1; 			%    user-defined time pick %
    tt(13) = 	sachead.t2; 			%    user-defined time pick %
    tt(14) = 	sachead.t3; 			%    user-defined time pick %
    tt(15) = 	sachead.t4; 			%    user-defined time pick %
    tt(16) = 	sachead.t5; 			%    user-defined time pick %
    tt(17) = 	sachead.t6; 			%    user-defined time pick %
    tt(18) = 	sachead.t7; 			%    user-defined time pick %
    tt(19) = 	sachead.t8; 			%    user-defined time pick %
    tt(20) = 	sachead.t9;			%    user-defined time pick %
    tt(21) = 	sachead.f; 			%    event end, sec > 0     %
    tt(22) = 	sachead.resp0; 		%    instrument respnse parm %
    tt(23) = 	sachead.resp1; 		%    instrument respnse parm %
    tt(24) = 	sachead.resp2; 		%    instrument respnse parm %
    tt(25) = 	sachead.resp3; 		%    instrument respnse parm %
    tt(26) = 	sachead.resp4; 		%    instrument respnse parm %
    tt(27) = 	sachead.resp5; 		%    instrument respnse parm %
    tt(28) = 	sachead.resp6; 		%    instrument respnse parm %
    tt(29) = 	sachead.resp7;		%    instrument respnse parm %
    tt(30) = 	sachead.resp8; 		%    instrument respnse parm %
    tt(31) = 	sachead.resp9; 		%    instrument respnse parm %
    tt(32) = 	sachead.stla; 		%  T station latititude      %
    tt(33) = 	sachead.stlo; 		%  T station longitude       %
    tt(34) = 	sachead.stel; 		%  T station elevation, m    %
    tt(35) = 	sachead.stdp; 		%  T station depth, m        %
    tt(36) = 	sachead.evla; 		%    event latitude          %
    tt(37) = 	sachead.evlo; 		%    event longitude         %
    tt(38) = 	sachead.evel; 		%    event elevation         %
    tt(39) = 	sachead.evdp; 		%    event depth             %
    tt(40) = 	sachead.mag; 	    %    reserved for future use %
    tt(41) = 	sachead.user0; 		%    available to user       %
    tt(42) = 	sachead.user1; 		%    available to user       %
    tt(43) = 	sachead.user2; 		%    available to user       %
    tt(44) = 	sachead.user3; 		%    available to user       %
    tt(45) = 	sachead.user4; 		%    available to user       %
    tt(46) = 	sachead.user5; 		%    available to user       %
    tt(47) = 	sachead.user6; 		%    available to user       %
    tt(48) = 	sachead.user7; 		%    available to user       %
    tt(49) = 	sachead.user8; 		%    available to user       %
    tt(50) = 	sachead.user9; 		%    available to user       %
    tt(51) = 	sachead.dist; 		%    stn-event distance, km  %
    tt(52) = 	sachead.az; 		%    event-stn azimuth       %
    tt(53) = 	sachead.baz; 		%    stn-event azimuth       %
    tt(54) = 	sachead.gcarc; 	    %    stn-event dist, degrees %
    tt(55) = 	sachead.internal2; 	%    internal use            %
    tt(56) = 	sachead.internal3; 	%    internal use            %
    tt(57) = 	sachead.depmen; 	%    mean value, amplitude   %
    tt(58) = 	sachead.cmpaz; 		%    component azimuth       %
    tt(59) = 	sachead.cmpinc; 	%    component inclination   %
    tt(60) = 	sachead.xminimum;   %    Minimum value of X (Spectral files only)	%
    tt(61) = 	sachead.xmaximum; 	%    Maximum value of X (Spectral files only)	%
    tt(62) = 	sachead.yminimum; 	%    Minimum value of Y (Spectral files only)	%
    tt(63) = 	sachead.ymaximum; 	%    Maximum value of Y (Spectral files only)	%
    tt(64) = 	sachead.unused6; 	%    reserved for future use %
    tt(65) = 	sachead.unused7; 	%    reserved for future use %
    tt(66) = 	sachead.unused8; 	%    reserved for future use %
    tt(67) = 	sachead.unused9; 	%    reserved for future use %
    tt(68) = 	sachead.unused10; 	%    reserved for future use %
    tt(69) = 	sachead.unused11; 	%    reserved for future use %
    tt(70) =    sachead.unused12;   %    reserved for future use %

    fwrite( fid, tt, 'single' );
 
%% --------------- Write out 40 integer header variables ---------------- %
% --------------------------- write 40 integers ------------------------- %
    tt = zeros( 1, 40, 'int32' );
 
	tt(1)  =    sachead.nzyear;
    tt(2)  = 	sachead.nzjday;
    tt(3)  = 	sachead.nzhour;
    tt(4)  = 	sachead.nzmin;
    tt(5)  = 	sachead.nzsec;
    tt(6)  = 	sachead.nzmsec;
    tt(7)  = 	sachead.nvhdr;
    tt(8)  = 	sachead.norid;
    tt(9)  = 	sachead.nevid;
    tt(10) = 	sachead.npts;
    tt(11) = 	sachead.internal7 ;
    tt(12) = 	sachead.nwfid;
    tt(13) = 	sachead.nxsize;
    tt(14) = 	sachead.nysize;
    tt(15) = 	sachead.unused15;
    tt(16) = 	sachead.iftype;
    tt(17) = 	sachead.idep;
    tt(18) = 	sachead.iztype;
    tt(19) = 	sachead.unused16;
    tt(20) = 	sachead.iinst;
    tt(21) = 	sachead.istreg;
    tt(22) = 	sachead.ievreg;
    tt(23) = 	sachead.ievtyp;
    tt(24) = 	sachead.iqual;
    tt(25) = 	sachead.isynth ;
    tt(26) = 	sachead.imagtyp;
    tt(27) = 	sachead.imagsrc;
    tt(28) = 	sachead.unused19;
    tt(29) = 	sachead.unused20;
    tt(30) = 	sachead.unused21;
    tt(31) = 	sachead.unused22;
    tt(32) = 	sachead.unused23;
    tt(33) = 	sachead.unused24;
    tt(34) = 	sachead.unused25;
    tt(35) = 	sachead.unused26;
    tt(36) = 	sachead.leven;
    tt(37) = 	sachead.lpspol;
    tt(38) = 	sachead.lovrok;
    tt(39) = 	sachead.lcalda;
    tt(40) = 	sachead.unused27;
    
    fwrite( fid, tt, 'int32' );

%% ------------- Write out 23 character header variables ---------------- %
% ------------------------ write 192 character -------------------------- %
	xx(1:8)      = 	sachead.kstnm; 		%  F station name           %;
    xx(9:24)     = 	sachead.kevnm; 		%    event name             %;
    xx(25:32)    = 	sachead.khole; 		%    man-made event name    %;
    xx(33:40)	 = 	sachead.ko; 		%    event origin time id   %;
    xx(41:48)	 = 	sachead.ka; 		%    1st arrival time ident %;
    xx(49:56)	 = 	sachead.kt0; 		%    time pick 0 ident      %;
    xx(57:64)	 = 	sachead.kt1; 		%    time pick 1 ident      %;
    xx(65:72)	 = 	sachead.kt2; 		%    time pick 2 ident      %;
    xx(73:80)	 = 	sachead.kt3; 		%    time pick 3 ident      %;
    xx(81:88)	 = 	sachead.kt4; 		%    time pick 4 ident      %;
    xx(89:96)	 = 	sachead.kt5; 	    %    time pick 5 ident      %;
    xx(97:104)	 = 	sachead.kt6; 	    %    time pick 6 ident      %;
    xx(105:112)	 = 	sachead.kt7; 		%    time pick 7 ident      %;
    xx(113:120)	 = 	sachead.kt8; 		%    time pick 8 ident      %;
    xx(121:128)	 = 	sachead.kt9; 		%    time pick 9 ident      %;
    xx(129:136)	 = 	sachead.kf; 		%    end of event ident     %;
    xx(137:144)  = 	sachead.kuser0;     %    available to user      %;
    xx(145:152)  = 	sachead.kuser1; 	%    available to user      %;
    xx(153:160)  = 	sachead.kuser2; 	%    available to user      %;
    xx(161:168)  = 	sachead.kcmpnm; 	%  F component name         %;
    xx(169:176)  = 	sachead.knetwk; 	%    network name           %;
    xx(177:184)  = 	sachead.kdatrd; 	%    date data read         %;
    xx(185:192)  = 	sachead.kinst; 	    %    instrument name        %;
	
    fwrite( fid, xx, 'char' );
    
%% ---------------------------------------------------------------------- %
% --------------------------- write data--------------------------------- %
	fwrite( fid, data, 'single');
    
% ----------------------------------------------------------------------- %
% --------------------------- close file id ----------------------------- %
fclose( fid );

end

1.3 Generate a Default SAC Header Structure

Be careful, you need to separately set the header variables nvhdr and iftype as 6 and 1 when you create a default SAC header structure. Here the SAC version number is 101.6a.

  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
% ----------------------------------------------------------------------- %
%           Create SAC header struct with default values.                 %
% ----------------------------------------------------------------------- %
%sachead: Output SAC header struct.

function sachead = defaultSACHeader()

%% Create 70 SAC header variables with float type.

	sachead.delta     = -12345;    %       time increment, sec       %
    sachead.depmin    = -12345;    %       minimum amplitude         %
    sachead.depmax    = -12345;    %       maximum amplitude         %
    sachead.scale     = -12345;    %       amplitude scale factor    %
    sachead.odelta    = -12345;    %       observed time inc         %
    sachead.b         = -12345;    %       initial time - wrt nz*    %
    sachead.e         = -12345;    %       end time                  %
    sachead.o         = -12345;    %       event start               %
    sachead.a         = -12345;    %       1st arrival time          %
    sachead.internal1 = -12345;    %       internal use              %
    sachead.t0        = -12345;    %       user-defined time pick    %
    sachead.t1        = -12345;    %       user-defined time pick    %
    sachead.t2        = -12345;    %       user-defined time pick    %
    sachead.t3        = -12345;    %       user-defined time pick    %
    sachead.t4        = -12345;    %       user-defined time pick    %
    sachead.t5        = -12345;    %       user-defined time pick    %
    sachead.t6        = -12345;    %       user-defined time pick    %
    sachead.t7        = -12345;    %       user-defined time pick    %
    sachead.t8        = -12345;    %       user-defined time pick    %
    sachead.t9        = -12345;    %       user-defined time pick    %
    sachead.f         = -12345;    %       event end, sec > 0        %
    sachead.resp0     = -12345;    %       instrument respnse parm    %
    sachead.resp1     = -12345;    %       instrument respnse parm    %
    sachead.resp2     = -12345;    %       instrument respnse parm    %
    sachead.resp3     = -12345;    %       instrument respnse parm    %
    sachead.resp4     = -12345;    %       instrument respnse parm    %
    sachead.resp5     = -12345;    %       instrument respnse parm    %
    sachead.resp6     = -12345;    %       instrument respnse parm    %
    sachead.resp7     = -12345;    %       instrument respnse parm    %
    sachead.resp8     = -12345;    %       instrument respnse parm    %
    sachead.resp9     = -12345;    %       instrument respnse parm    %
    sachead.stla      = -12345;    %     T station latititude         %
    sachead.stlo      = -12345;    %     T station longitude          %
    sachead.stel      = -12345;    %     T station elevation, m       %
    sachead.stdp      = -12345;    %     T station depth, m           %
    sachead.evla      = -12345;    %       event latitude             %
    sachead.evlo      = -12345;    %       event longitude            %
    sachead.evel      = -12345;    %       event elevation            %
    sachead.evdp      = -12345;    %       event depth                %
    sachead.mag       = -12345;    %       reserved for future use    %
    sachead.user0     = -12345;    %       available to user          %
    sachead.user1     = -12345;    %       available to user          %
    sachead.user2     = -12345;    %       available to user          %
    sachead.user3     = -12345;    %       available to user          %
    sachead.user4     = -12345;    %       available to user          %
    sachead.user5     = -12345;    %       available to user          %
    sachead.user6     = -12345;    %       available to user          %
    sachead.user7     = -12345;    %       available to user          %
    sachead.user8     = -12345;    %       available to user          %
    sachead.user9     = -12345;    %       available to user          %
    sachead.dist      = -12345;    %       stn-event distance, km     %
    sachead.az        = -12345;    %       event-stn azimuth          %
    sachead.baz       = -12345;    %       stn-event azimuth          %
    sachead.gcarc     = -12345;    %       stn-event dist, degrees    %
    sachead.internal2 = -12345;    %       internal use               %
    sachead.internal3 = -12345;    %       internal use               %
    sachead.depmen    = -12345;    %       mean value, amplitude      %
    sachead.cmpaz     = -12345;    %       component azimuth          %
    sachead.cmpinc    = -12345;    %       component inclination      %
    sachead.xminimum  = -12345;    %       Minimum value of X (Spectral files only   %
    sachead.xmaximum  = -12345;    %       Maximum value of X (Spectral files only)  %
    sachead.yminimum  = -12345;    %       Minimum value of Y (Spectral files only)  %
    sachead.ymaximum  = -12345;    %       Maximum value of Y (Spectral files only)  %
    sachead.unused6   = -12345;    %       reserved for future use    %
    sachead.unused7   = -12345;    %       reserved for future use    %
    sachead.unused8   = -12345;    %       reserved for future use    %
    sachead.unused9   = -12345;    %       reserved for future use    %
    sachead.unused10  = -12345;    %       reserved for future use    %
    sachead.unused11  = -12345;    %       reserved for future use    %
    sachead.unused12  = -12345;    %       reserved for future use    %
 
%% ------------ Create 40 integer header variables --------------- %
    
	sachead.nzyear     = -12345;
    sachead.nzjday     = -12345;
    sachead.nzhour     = -12345;
    sachead.nzmin      = -12345;
    sachead.nzsec      = -12345;
    sachead.nzmsec     = -12345;
    sachead.nvhdr      =      6;
    sachead.norid      = -12345;
    sachead.nevid      = -12345;
    sachead.npts       = -12345;
    sachead.internal7  = -12345;
    sachead.nwfid      = -12345;
    sachead.nxsize     = -12345;
    sachead.nysize     = -12345;
    sachead.unused15   = -12345;
    sachead.iftype     =      1;
    sachead.idep       = -12345;
    sachead.iztype     = -12345;
    sachead.unused16   = -12345;
    sachead.iinst      = -12345;
    sachead.istreg     = -12345;
    sachead.ievreg     = -12345;
    sachead.ievtyp     = -12345;
    sachead.iqual      = -12345;
    sachead.isynth     = -12345;
    sachead.imagtyp    = -12345;
    sachead.imagsrc    = -12345;
    sachead.unused19   = -12345;
    sachead.unused20   = -12345;
    sachead.unused21   = -12345;
    sachead.unused22   = -12345;
    sachead.unused23   = -12345;
    sachead.unused24   = -12345;
    sachead.unused25   = -12345;
    sachead.unused26   = -12345;
    sachead.leven      =      1;
    sachead.lpspol     = -12345;
    sachead.lovrok     = -12345;
    sachead.lcalda     =      1;
    sachead.unused27   = -12345;   

%% ------------ Create 23 character header variables -------------- %
% ------------------------ write 192 character --------------%------------ %
	sachead.kstnm  = ('-12345  ')'; 		   %  F station name           %;
    sachead.kevnm  = ('-12345          ')';    %    event name             %;
    sachead.khole  = ('-12345  ')'; 		   %    man-made event name    %;
    sachead.ko     = ('-12345  ')'; 		   %    event origin time id   %;
    sachead.ka     = ('-12345  ')'; 		   %    1st arrival time ident %;
    sachead.kt0    = ('-12345  ')'; 		   %    time pick 0 ident      %;
    sachead.kt1    = ('-12345  ')'; 		   %    time pick 1 ident      %;
    sachead.kt2    = ('-12345  ')'; 		   %    time pick 2 ident      %;
    sachead.kt3    = ('-12345  ')'; 		   %    time pick 3 ident      %;
    sachead.kt4    = ('-12345  ')'; 		   %    time pick 4 ident      %;
    sachead.kt5    = ('-12345  ')'; 	       %    time pick 5 ident      %;
    sachead.kt6    = ('-12345  ')'; 	       %    time pick 6 ident      %;
    sachead.kt7    = ('-12345  ')'; 		   %    time pick 7 ident      %;
    sachead.kt8    = ('-12345  ')'; 		   %    time pick 8 ident      %;
    sachead.kt9    = ('-12345  ')'; 		   %    time pick 9 ident      %;
    sachead.kf     = ('-12345  ')'; 		   %    end of event ident     %;
    sachead.kuser0 = ('-12345  ')';            %    available to user      %;
    sachead.kuser1 = ('-12345  ')'; 	       %    available to user      %;
    sachead.kuser2 = ('-12345  ')'; 	       %    available to user      %;
    sachead.kcmpnm = ('-12345  ')'; 	       %  F component name         %;
    sachead.knetwk = ('-12345  ')'; 	       %    network name           %;
    sachead.kdatrd = ('-12345  ')'; 	       %    date data read         %;
    sachead.kinst  = ('-12345  ')'; 	       %    instrument name        %;
end

1.4 Demo of Write out SAC File with New Data

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
n = 5001;
dt = 1e-2;
t = (0: n-1) * dt;
s = sin( 2 * pi * t );
hd = defaultSACHeader();
hd.npts = n;
hd.delta = dt;
hd.b = t(1); hd.e = t(end);
hd.depmax = max(s); hd.depmin = min(s); hd.depmen = mean(s); 
writeSAC('Example2.SAC', hd, s);