Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring ccftime plot to use objects #103

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 40 additions & 26 deletions msnoise/plots/ccftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
"""
# plot interferogram

import datetime

import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Cursor

from obspy.signal.filter import envelope as obspy_envelope
from obspy.signal.filter import bandpass
from ..api import *
from obspy.signal.filter import envelope as obspy_envelope

from msnoise.api import connect, get_config, get_maxlag_samples, \
build_movstack_datelist, get_filters, get_results


def main(sta1, sta2, filterid, components, mov_stack=1, ampli=5, seismic=False,
Expand All @@ -33,11 +37,13 @@ def main(sta1, sta2, filterid, components, mov_stack=1, ampli=5, seismic=False,
samples = get_maxlag_samples(db)
cc_sampling_rate = float(get_config(db, 'cc_sampling_rate'))
start, end, datelist = build_movstack_datelist(db)
base = mdates.date2num(start)
plt.figure(figsize=(12, 9))
base = mdates.date2num(start)

fig = plt.figure(figsize=(12, 9))

sta1 = sta1.replace('.', '_')
sta2 = sta2.replace('.', '_')
t = np.arange(samples)/cc_sampling_rate - maxlag
t = np.arange(samples) / cc_sampling_rate - maxlag

if refilter:
freqmin, freqmax = refilter.split(':')
Expand All @@ -46,49 +52,54 @@ def main(sta1, sta2, filterid, components, mov_stack=1, ampli=5, seismic=False,

if sta2 >= sta1:
pair = "%s:%s" % (sta1, sta2)

print("New Data for %s-%s-%i-%i" % (pair, components, filterid,
mov_stack))
nstack, stack_total = get_results(db, sta1, sta2, filterid, components,
datelist, mov_stack, format="matrix")
ax = plt.subplot(111)
ax = fig.add_subplot(111)
for i, line in enumerate(stack_total):
if np.all(np.isnan(line)):
continue

if refilter:
line = bandpass(line, freqmin, freqmax, cc_sampling_rate,
zerophase=True)
if envelope:
line = obspy_envelope(line)

line /= line.max()
plt.plot(t, line * ampli + i + base, c='k')
ax.plot(t, line * ampli + i + base, c='k')

if seismic:
y1 = np.ones(len(line)) * i
y2 = line*ampli + i + base
plt.fill_between(t, y1, y2, where=y2 >= y1, facecolor='k',
interpolate=True)
y2 = line * ampli + i + base
ax.fill_between(t, y1, y2, where=y2 >= y1, facecolor='k',
interpolate=True)

for filterdb in get_filters(db, all=True):
if filterid == filterdb.ref:
low = float(filterdb.low)
high = float(filterdb.high)
break

plt.xlabel("Lag Time (s)")
plt.axhline(0, lw=0.5, c='k')
plt.grid()
title = '%s : %s, %s, Filter %d (%.2f - %.2f Hz), Stack %d' %\

ax.set_xlabel("Lag Time (s)")
ax.axhline(0, lw=0.5, c='k')
ax.grid()
ax.scatter(0, [start, ], alpha=0)
ax.set_ylim(start - datetime.timedelta(days=ampli),
end + datetime.timedelta(days=ampli))
ax.set_xlim(-maxlag, maxlag)
ax.fmt_ydata = mdates.DateFormatter('%Y-%m-%d')
cursor = Cursor(ax, useblit=True, color='red', linewidth=1.2)

title = '%s : %s, %s, Filter %d (%.2f - %.2f Hz), Stack %d' % \
(sta1.replace('_', '.'), sta2.replace('_', '.'), components,
filterid, low, high, mov_stack)

if refilter:
title += ", Re-filtered (%.2f - %.2f Hz)" % (freqmin, freqmax)
plt.title(title)
plt.scatter(0, [start, ], alpha=0)
plt.ylim(start-datetime.timedelta(days=ampli),
end+datetime.timedelta(days=ampli))
plt.xlim(-maxlag, maxlag)
ax.fmt_ydata = mdates.DateFormatter('%Y-%m-%d')
cursor = Cursor(ax, useblit=True, color='red', linewidth=1.2)
ax.set_title(title)

if outfile:
if outfile.startswith("?"):
Expand All @@ -99,6 +110,9 @@ def main(sta1, sta2, filterid, components, mov_stack=1, ampli=5, seismic=False,
mov_stack))
outfile = "ccftime " + outfile
print("output to:", outfile)
plt.savefig(outfile)
fig.savefig(outfile)

if show:
plt.show()
fig.show()
else:
plt.close(fig)