Académique Documents
Professionnel Documents
Culture Documents
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
RESP5
RESP6
RESP7
RESP8
RESP9
STLA
STLO
STEL
STDP
EVLA
EVLO
EVEL
EVDP
MAG
USER0
USER1
USER2
USER3
USER4
USER5
USER6
USER7
USER8
USER9
DIST
AZ
BAZ
GCARC
INTERNAL2
INTERNAL3
DEPMEN
CMPAZ
CMPINC
XMINIMUM
XMAXIMUM
YMINIMUM
YMAXIMUM
UNUSED1
UNUSED2
UNUSED3
UNUSED4
UNUSED5
UNUSED6
UNUSED7
NZYEAR
NZJDAY
NZHOUR
NZMIN
NZSEC
NZMSEC
NVHDR
NORID
NEVID
NPTS
INTERNAL4
NWFID
NXSIZE
%
NYSIZE
- Spectral width (spectral file only)
%
UNUSED8
- Eighth unused variable
%
IFTYPE
- Type of file
%
IDEP
- Type of dependent variable
%
IZTYPE
- Reference-time equivalence
%
UNUSED9
- Ninth unused variable
%
IINST
- Type of recording instrument
%
ISTREG
- Station geographic region
%
IEVREG
- Event geographic region
%
IEVTYP
- Type of event
%
IQUAL
- Quality of data
%
ISYNTH
- Synthetic data flag
%
IMAGTYP
- Magnitude type
%
IMAGSRC
- Magnitude source
%
UNUSED10
- Tenth unused variable
%
UNUSED11
- Eleventh unused variable
%
UNUSED12
- Twelveth unused variable
%
UNUSED13
- Thirteenth unused variable
%
UNUSED14
- Fourteenth unused variable
%
UNUSED15
- Fifteenth unused variable
%
UNUSED16
- Sixteenth unused variable
%
UNUSED17
- Seventeenth unused variable
%
LEVEN
- True if data is evenly spaced
%
LPSPOL
- True if station polarity follows left-hand rule
%
LOVROK
- True if it is ok to overwrite this file on disk
%
LCALDA
- True if DIST, AZ, BAZ and GCARC are to be calculated from
%
station and event coordinates
%
UNUSED18
- Eighteenth unused variable
%
KSTNM
- Station name
%
KEVNM
- Event name
%
KHOLE
- Hole ID for nuclear test
%
KO
- Event origin time ID
%
KA
- First arrival time ID
%
KT0
- First user-defined time pick ID
%
KT1
- Second user-defined time pick ID
%
KT2
- Third user-defined time pick ID
%
KT3
- Fourth user-defined time pick ID
%
KT4
- Fifth user-defined time pick ID
%
KT5
- Sixth user-defined time pick ID
%
KT6
- Seventh user-defined time pick ID
%
KT7
- Eighth user-defined time pick ID
%
KT8
- Ninth user-defined time pick ID
%
KT9
- Tenth user-defined time pick ID
%
KF
- Final or end event time ID
%
KUSER0
- First user-defined text string
%
KUSER1
- Second user-defined text string
%
KUSER2
- Third user-defined text string
%
KCMPNM
- Component name
%
KNETWK
- Network name
%
KDATRD
- Date data were read onto computer
%
KINST
- Generic name of recording instrument
%
DATA1
- First data block
%
% If the SAC data file is for spectral or unevenly-spaced xy data,
% following fields may exist.
%
%
DATA2
- Second data block
%
% Values of enumerated I-type header variables are converted from integers
% to corresponding alphanumeric names (all upper case).
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
*************************************************************************
This program was developed originally from the program readsac.m of the
package MatSeis 1.6 by Sandia National Laboratories.
Mark Harris, mharris@sandia.gov
Copyright (c) 1996-2001 Sandia National Laboratories. All rights
reserved.
*************************************************************************
Copyright, 19, The Board of Governors of the Los Alamos National
Security, LLC. This software was produced under a U. S. Government
contract (DE-AC52-06NA25396) by Los Alamos National Laboratory, which is
operated by the Los Alamos National Security, LLC for the U. S.
Department of Energy. The U. S. Government is licensed to use, reproduce,
and distribute this software. Permission is granted to the public to copy
and use this software without charge, provided that this Notice and any
statement of authorship are reproduced on all copies. Neither the
Government nor the LANS makes any warranty, express or implied, or
assumes any liability or responsibility for the use of this software.
Xiaoning Yang, xyang@lanl.gov
end
else
filename_temp(i) = {filenames(i)};
end
end
filenames = [filename_temp{:}];
end
nfiles = length(filenames);
s(f).INTERNAL3 = A(56);
s(f).DEPMEN = A(57);
s(f).CMPAZ = A(58);
s(f).CMPINC = A(59);
s(f).XMINIMUM = A(60);
s(f).XMAXIMUM = A(61);
s(f).YMINIMUM = A(62);
s(f).YMAXIMUM = A(63);
s(f).UNUSED1 = A(64);
s(f).UNUSED2 = A(65);
s(f).UNUSED3 = A(66);
s(f).UNUSED4 = A(67);
s(f).UNUSED5 = A(68);
s(f).UNUSED6 = A(69);
s(f).UNUSED7 = A(70);
s(f).NZYEAR = B(1);
s(f).NZJDAY = B(2);
s(f).NZHOUR = B(3);
s(f).NZMIN = B(4);
s(f).NZSEC = B(5);
s(f).NZMSEC = B(6);
s(f).NVHDR = B(7);
s(f).NORID = B(8);
s(f).NEVID = B(9);
s(f).NPTS = B(10);
s(f).INTERNAL4 = B(11);
s(f).NWFID = B(12);
s(f).NXSIZE = B(13);
s(f).NYSIZE = B(14);
s(f).UNUSED8 = B(15);
switch B(16)
case 1, filetype = 'ITIME';
case 2, filetype = 'IRLIM';
case 3, filetype = 'IAMPH';
case 4, filetype = 'IXY';
case 51, filetype = 'IXYZ';
end
s(f).IFTYPE = filetype;
switch B(17)
case 5, sigtype = 'IUNKN';
case 6, sigtype = 'IDISP';
case 7, sigtype = 'IVEL';
case 8, sigtype = 'IACC';
case 50, sigtype = 'IVOLTS';
otherwise, sigtype = '';
end
s(f).IDEP = sigtype;
switch B(18)
case 5, refequiv = 'IUNKN';
case 9, refequiv = 'IB';
case 10, refequiv = 'IDAY';
case 11, refequiv = 'IO';
case 12, refequiv = 'IA';
case 13, refequiv = 'IT0';
case 14, refequiv = 'IT1';
case 15, refequiv = 'IT2';
case 16, refequiv = 'IT3';
'IT4';
'IT5';
'IT6';
'IT7';
'IT8';
'IT9';
= '';
end
s(f).IZTYPE = refequiv;
s(f).UNUSED9 = B(19);
s(f).IINST = B(20);
s(f).ISTREG = B(21);
s(f).IEVREG = B(22);
switch B(23)
case 5, evtype = 'IUNKN';
case 37, evtype = 'INUCL';
case 38, evtype = 'IPREN';
case 39, evtype = 'IPOSTN';
case 40, evtype = 'IQUAKE';
case 41, evtype = 'IPREQ';
case 42, evtype = 'IPOSTQ';
case 43, evtype = 'ICHEM';
case 44, evtype = 'IOTHER';
case 70, evtype = 'IQB';
case 71, evtype = 'IQB1';
case 72, evtype = 'IQB2';
case 73, evtype = 'IQBX';
case 74, evtype = 'IQMT';
case 75, evtype = 'IEQ';
case 76, evtype = 'IEQ1';
case 77, evtype = 'IEQ2';
case 78, evtype = 'IME';
case 79, evtype = 'IEX';
case 80, evtype = 'INU';
case 81, evtype = 'INC';
case 82, evtype = 'IO_';
case 83, evtype = 'IL';
case 84, evtype = 'IR';
case 85, evtype = 'IT';
case 86, evtype = 'IU';
otherwise, evtype = '';
end
s(f).IEVTYP = evtype;
switch B(24)
case 44, quality = 'IOTHER';
case 45, quality = 'IGOOD';
case 46, quality = 'IGLCH';
case 47, quality = 'IDROP';
case 48, quality = 'ILOWSN';
otherwise, quality = '';
end
s(f).IQUAL = quality;
switch B(25)
case 49, synth = 'IRLDTA';
otherwise, synth = '';
end
s(f).ISYNTH = synth;
switch B(26)
case 52, magtype = 'IMB';
case 53, magtype = 'IMS';
case 54, magtype = 'IML';
case 55, magtype = 'IMW';
case 56, magtype = 'IMD';
case 57, magtype = 'IMX';
otherwise, magtype = '';
end
s(f).IMAGTYP = magtype;
switch B(27)
case 58, magsrc = 'INEIC';
case 59, magsrc = 'IPDE';
case 60, magsrc = 'IISC';
case 61, magsrc = 'IREB';
case 62, magsrc = 'IUSGS';
case 63, magsrc = 'IBRK';
case 64, magsrc = 'ICALTECH';
case 65, magsrc = 'ILLNL';
case 66, magsrc = 'IEVLOC';
case 67, magsrc = 'IJSOP';
case 68, magsrc = 'IUSER';
case 69, magsrc = 'IUNKNOWN';
otherwise, magsrc = '';
end
s(f).IMAGSRC = magsrc;
s(f).UNUSED10 = B(28);
s(f).UNUSED11 = B(29);
s(f).UNUSED12 = B(30);
s(f).UNUSED13 = B(31);
s(f).UNUSED14 = B(32);
s(f).UNUSED15 = B(33);
s(f).UNUSED16 = B(34);
s(f).UNUSED17 = B(35);
s(f).LEVEN = logical(B(36));
if ~isnan(B(37))
s(f).LPSPOL = logical(B(37));
else
s(f).LPSPOL = B(37);
end
if ~isnan(B(38))
s(f).LOVROK = logical(B(38));
else
s(f).LOVROK = B(38);
end
if ~isnan(B(39))
s(f).LCALDA = logical(B(39));
else
s(f).LCALDA = B(39);
end
if ~isnan(B(40))
s(f).UNUSED18 = logical(B(40));
else
s(f).UNUSED18 = B(40);
end
s(f).KSTNM = C{1};
s(f).KEVNM = [C{2:3}];
s(f).KHOLE = C{4};
s(f).KO = C{5};
s(f).KA = C{6};
s(f).KT0 = C{7};
s(f).KT1 = C{8};
s(f).KT2 = C{9};
s(f).KT3 = C{10};
s(f).KT4 = C{11};
s(f).KT5 = C{12};
s(f).KT6 = C{13};
s(f).KT7 = C{14};
s(f).KT8 = C{15};
s(f).KT9 = C{16};
s(f).KF = C{17};
s(f).KUSER0 = C{18};
s(f).KUSER1 = C{19};
s(f).KUSER2 = C{20};
s(f).KCMPNM = C{21};
s(f).KNETWK = C{22};
s(f).KDATRD = C{23};
s(f).KINST = C{24};
% read data blocks
clear count2
[D, count1] = fread(fid, [s(f).NPTS, 1], 'float32');
s(f).DATA1 = D;
if ~s(f).LEVEN || strcmp(s(f).IFTYPE, 'IAMPH') || ...
strcmp(s(f).IFTYPE, 'IRLIM')
[D, count2] = fread(fid, [s(f).NPTS, 1], 'float32');
s(f).DATA2 = D;
end
% flag successfully read SAC file
if count1 == s(f).NPTS
if exist('count2','var')
if count2 == s(f).NPTS
count(f) = true;
end
else
count(f) = true;
end
end
end
% close SAC file.
fclose(fid);
end
end
s = s(count);
% assign output
if nargout == 1
t1 = s;
elseif nargout == 2
[t1, t2] = getsacdata(s);
else
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function s = sacstruct(n)
% S = SACSTRUCT(N); returns an Nx1 SAC structure with all fields
% initiated to their undefined (default) values. N defaults to 1.
%
%
%
% check arguments.
if (nargin < 1)
n = 1;
end
% initialize output structure.
sacfields = {'FILENAME'; 'DELTA'; 'DEPMIN'; 'DEPMAX'; 'SCALE'; ...
'ODELTA'; 'B'; 'E'; 'O'; 'A'; 'INTERNAL1'; 'T0'; 'T1'; 'T2'; 'T3'; ...
'T4'; 'T5'; 'T6'; 'T7'; 'T8'; 'T9'; 'F'; 'RESP0'; 'RESP1'; 'RESP2'; ...
'RESP3'; 'RESP4'; 'RESP5'; 'RESP6'; 'RESP7'; 'RESP8'; 'RESP9'; ...
'STLA'; 'STLO'; 'STEL'; 'STDP'; 'EVLA'; 'EVLO'; 'EVEL'; 'EVDP'; ...
'MAG'; 'USER0'; 'USER1'; 'USER2'; 'USER3'; 'USER4'; 'USER5'; ...
'USER6'; 'USER7'; 'USER8'; 'USER9'; 'DIST'; 'AZ'; 'BAZ'; 'GCARC'; ...
'INTERNAL2'; 'INTERNAL3'; 'DEPMEN'; 'CMPAZ'; 'CMPINC'; 'XMINIMUM'; ...
'XMAXIMUM'; 'YMINIMUM'; 'YMAXIMUM'; 'UNUSED1'; 'UNUSED2'; ...
'UNUSED3'; 'UNUSED4'; 'UNUSED5'; 'UNUSED6'; 'UNUSED7'; 'NZYEAR'; ...
'NZJDAY'; 'NZHOUR'; 'NZMIN'; 'NZSEC'; 'NZMSEC'; 'NVHDR'; 'NORID'; ...
'NEVID'; 'NPTS'; 'INTERNAL4'; 'NWFID'; 'NXSIZE'; 'NYSIZE'; ...
'UNUSED8'; 'IFTYPE'; 'IDEP'; 'IZTYPE'; 'UNUSED9'; 'IINST'; ...
'ISTREG'; 'IEVREG'; 'IEVTYP'; 'IQUAL'; 'ISYNTH'; 'IMAGTYP'; ...
'IMAGSRC'; 'UNUSED10'; 'UNUSED11'; 'UNUSED12'; 'UNUSED13'; ...
'UNUSED14'; 'UNUSED15'; 'UNUSED16'; 'UNUSED17'; 'LEVEN'; 'LPSPOL'; ...
'LOVROK'; 'LCALDA'; 'UNUSED18'; 'KSTNM'; 'KEVNM'; 'KHOLE'; 'KO'; ...
'KA'; 'KT0'; 'KT1'; 'KT2'; 'KT3'; 'KT4'; 'KT5'; 'KT6'; 'KT7'; ...
'KT8'; 'KT9'; 'KF'; 'KUSER0'; 'KUSER1'; 'KUSER2'; 'KCMPNM'; ...
'KNETWK'; 'KDATRD'; 'KINST'; 'DATA1';};
cl = cell(size(sacfields,1),n);
cl(2:111, :) = {nan};
cl(112:end-1, :) = {' '};
s = cell2struct(cl, sacfields, 1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [out1, out2, out3] = getsacdata(s)
% [INDEP, DEP] = GETSACDATA(S); extracts independent and dependent
% variables from SAC structure S and puts them in INDEP and DEP. INDEP and
% DEP can either be time and data or x and y. They are cell arrays if
% length(S) > 1 and the lengths of the variables for different S elements
% are different, or vectors (matrices) if either length(S) == 1 or the
% lengths of the variables for different S elements are the same. If S
% contains time series, the independent variable (time) is absolute time
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
% loop over s
ll = zeros(l, 1); % lengths of time series
for i = 1:l
ll(i) = s(i).NPTS;
if i == 1
file_type = s(i).IFTYPE;
% reference time for all traces
if strcmp(file_type, 'ITIME')
year0 = s(i).NZYEAR;
days0 = s(i).NZJDAY;
ref0 = days0*86400+s(i).NZHOUR*3600+s(i).NZMIN*60+...
s(i).NZSEC+s(i).NZMSEC/1000;
if isnan(ref0)
end
ref0 = 0;
end
end
% check file type
if ~strcmp(s(i).IFTYPE, file_type)
error(' File types in input structure must be same !!!')
end
if strcmp(file_type, 'IXYZ')
if nargout ~= 3
error(' Number of output arguments must be three !!!')
end
out1(i) = {linspace(s(i).XMINIMUM, s(i).XMAXIMUM, s(i).NXSIZE)};
out2(i) = {linspace(s(i).YMINIMUM, s(i).YMAXIMUM, s(i).NYSIZE)'};
out3(i) = {reshape(s(i).DATA1, s(i).NXSIZE, s(i).NYSIZE)'};
elseif strcmp(file_type, 'IXY') && ~s(i).LEVEN
if nargout ~= 2
error(' Number of output arguments must be two !!!')
end
out1(i) = {s(i).DATA2};
% see SAC Manual
out2(i) = {s(i).DATA1};
else
% spectrum and evenly-spaced time series
if isnan(s(i).B)
B = 0;
else
B = s(i).B;
end
if strcmp(file_type, 'ITIME')
if isnan(s(i).O)
% reference time of this trace
year = s(i).NZYEAR;
year_diff = year-year0;
add_sec = 0;
if year_diff ~= 0
if year_diff > 0
ny = linspace(year0, year-1, year_diff);
for j = 1:length(ny)
if isleap(ny(j))
add_sec = add_sec+86400*366;
else
add_sec = add_sec+86400*365;
end
end
else
ny = linspace(year, year0-1, abs(year_diff));
for j = 1:length(ny)
if isleap(ny(j))
add_sec = add_sec-86400*366;
else
add_sec = add_sec-86400*365;
end
end
end
end
days = s(i).NZJDAY;
ref = days*86400+s(i).NZHOUR*3600+s(i).NZMIN*60+...
s(i).NZSEC+s(i).NZMSEC/1000+add_sec;
if isnan(ref)
ref = 0;
end
if isnan(B)
B = 0;
else
B = B+ref-ref0;
end
O = 0;
else
O = s(i).O;
end
out1(i) = {(0:s(i).NPTS-1)'*s(i).DELTA+B-O};
elseif strcmp(file_type, 'IXY')
out1(i) = {(0:s(i).NPTS-1)'*s(i).DELTA+B};
else % frequency vector for positive-frequency part of the spectrum
if B ~= 0
warning('MATLAB:getsacdata:FirstFPoint', ['First ', ...
'frequency point is not zero Hz !!!'])
end
npts = round(s(i).NPTS/2);
if s(i).NPTS/2 ~= npts
warning('MATLAB:getsacdata:NumberOfData', [' Spectrum', ...
' has odd number of data points !!!'])
end
out1(i) = {(0:npts)'*s(i).DELTA+B};
end
if (strcmp(file_type, 'ITIME') || strcmp(file_type, 'IXY')) && ...
nargout ~= 2
error(' Number of output arguments must be two !!!')
elseif (strcmp(file_type, 'IAMPH') || ...
strcmp(file_type, 'IRLIM')) && nargout ~= 3
error(' Number of output arguments must be three !!!')
else
out2(i) = {s(i).DATA1};
if strcmp(file_type, 'IAMPH') || strcmp(file_type, 'IRLIM')
out3(i) = {s(i).DATA2};
end
end
end
end
if l == 1
out1 = out1{:};
out2 = out2{:};
if nargout == 3
out3 = out3{:};
end
elseif ~any(diff(ll))