Skip to content

Commit

Permalink
Fix to GPS Clock-Correction Bug
Browse files Browse the repository at this point in the history
* Ensure clock-corrections are correctly applied on traces longer than a
  day
* The default value for --read-buffer-size is now the same as
  INTERVAL_SECONDS in correlator.py
  • Loading branch information
geojunky committed Nov 23, 2021
1 parent b9833f7 commit 0a04532
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 35 deletions.
94 changes: 61 additions & 33 deletions seismic/ASDFdatabase/_FederatedASDFDataSetImpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ def _get_correction(self, net, sta, st, et):
# end if

cst, cet = self.correction_map_bounds[net][sta][indices[0]]
a = np.fmax(st, cst)
b = np.fmin(et, cet)
a = np.fmax(st.timestamp, cst)
b = np.fmin(et.timestamp, cet)

if(a > b):
raise ValueError('Error encountered in _get_correction. Aborting..')
Expand All @@ -223,40 +223,68 @@ def _get_correction(self, net, sta, st, et):
#end func

def _apply_correction(self, stream):
resultStream = Stream()
for tr in stream:
net = tr.stats.network
sta = tr.stats.station
st = tr.stats.starttime
et = tr.stats.endtime

if(st == et):
resultStream.append(tr)
continue
# end if

result = self._get_correction(net, sta, st, et)

def day_split(trc):
stream = Stream()
step = 24*3600 # seconds

st = trc.stats.starttime
et = trc.stats.endtime
dayAlignedStartTime = UTCDateTime(year=st.year, month=st.month, day=st.day)
ct = dayAlignedStartTime

trcCopy = trc.copy()
while(ct < et):
if(ct + step > et):
step = et - ct
# end if

stream += trcCopy.slice(ct, ct + step)

if(result):
ost, oet = result[0]
corr = result[1]
ct += step
# wend

trCorrected = tr.copy().slice(ost, oet)
trCorrected.stats.starttime -= corr
currStream = Stream([tr.slice(st, ost),
trCorrected,
tr.slice(oet, et)])
return stream
#end func

# overlap resulting from correction applied is discarded (methode=0)
currStream.merge()
if(len(currStream) == 1):
resultStream.append(currStream[0])
resultStream = Stream()
for mtr in stream:
dayStream = day_split(mtr)

for tr in dayStream:
net = tr.stats.network
sta = tr.stats.station
st = tr.stats.starttime
et = tr.stats.endtime

if(st == et):
resultStream.append(tr)
continue
# end if

result = self._get_correction(net, sta, st, et)

if(result):
ost, oet = result[0]
corr = result[1]

trCorrected = tr.copy().slice(ost, oet)
trCorrected.stats.starttime -= corr
currStream = Stream([tr.slice(st, ost),
trCorrected,
tr.slice(oet, et)])

# overlap resulting from correction applied is discarded (method=0)
currStream.merge()
if(len(currStream) == 1):
resultStream.append(currStream[0])
else:
raise ValueError('Merge error in _apply_correction')
# end if
else:
raise ValueError('Merge error in _apply_correction')
resultStream.append(tr)
# end if
else:
resultStream.append(tr)
# end if
# end for
# end for

return resultStream
Expand All @@ -272,9 +300,9 @@ def decode_tag(tag, type='raw_recording'):
tokens = tokens[3].split('__')
cc = tokens[0]
starttime = UTCDateTime(tokens[1]).timestamp
endttime = UTCDateTime(tokens[2]).timestamp
endtime = UTCDateTime(tokens[2]).timestamp

return nc, sc, lc, cc, starttime, endttime
return nc, sc, lc, cc, starttime, endtime
except Exception:
if self.logger:
self.logger.error("Failed to decode tag {}".format(tag))
Expand Down
4 changes: 2 additions & 2 deletions seismic/xcorqc/correlator.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,9 @@ def evaluate_channels(cha1, cha2):
@click.option('--one-bit-normalize', is_flag=True,
help="Apply one-bit normalization to data in each window. Note that the default time-domain normalization "
"is N(0,1), i.e. 0-mean and unit variance")
@click.option('--read-buffer-size', default=10,
@click.option('--read-buffer-size', default=1,
type=int,
help="Data read buffer size; default is 10 x 'interval_seconds'. This parameter allows fetching data in bulk,"
help="Data read buffer size; default is 1 x 'interval_seconds'. This parameter allows fetching data in bulk,"
" which can improve efficiency, but has no effect on the results produced")
@click.option('--ds1-zchan', default='BHZ',
type=str,
Expand Down

0 comments on commit 0a04532

Please sign in to comment.