Skip to content

Commit 45f8722

Browse files
committed
tests successful - time for 0.1.1
1 parent 702f619 commit 45f8722

8 files changed

+287
-151
lines changed

obstools/atacr/plotting.py

+49-38
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,8 @@ def fig_comply(f, day_comps, day_list, sta_comps, sta_list, sta, f_0):
397397
compliance = np.abs(day_comps[i][key][0])
398398
ax.plot(f, compliance, 'gray', alpha=0.3, lw=0.5)
399399
ax.set_xlim(f_0, f_c)
400-
ytop = np.max(compliance[(f>f_0) & (f<f_c)])
401-
ybot = np.min(compliance[(f>f_0) & (f<f_c)])
400+
ytop = np.max(compliance[(f > f_0) & (f < f_c)])
401+
ybot = np.min(compliance[(f > f_0) & (f < f_c)])
402402
ax.set_ylim(ybot, ytop)
403403

404404
if sta_list[key]:
@@ -473,36 +473,41 @@ def fig_event_raw(evstream, fmin, fmax):
473473
sr = evstream.sth[0].stats.sampling_rate
474474
taxis = np.arange(0., 7200., 1./sr)
475475

476-
plt.figure(figsize=(6, 6))
477-
478-
plt.subplot(411)
479-
plt.plot(taxis, evstream.sth[0].data, 'k', lw=0.5)
480-
plt.title(evstream.key+' '+evstream.tstamp +
481-
': H1', fontdict={'fontsize': 8})
482-
plt.gca().ticklabel_format(axis='y', style='sci', useOffset=True,
483-
scilimits=(-3, 3))
484-
plt.xlim((0., 7200.))
485-
486-
plt.subplot(412)
487-
plt.plot(taxis, evstream.sth[1].data, 'k', lw=0.5)
488-
plt.xlim((0., 7200.))
489-
plt.title(evstream.tstamp+': H2', fontdict={'fontsize': 8})
490-
plt.gca().ticklabel_format(axis='y', style='sci', useOffset=True,
491-
scilimits=(-3, 3))
492-
493-
plt.subplot(413)
494-
plt.plot(taxis, evstream.sth[2].data, 'k', lw=0.5)
495-
plt.xlim((0., 7200.))
496-
plt.title(evstream.tstamp+': Z', fontdict={'fontsize': 8})
497-
plt.gca().ticklabel_format(axis='y', style='sci', useOffset=True,
498-
scilimits=(-3, 3))
499-
500-
plt.subplot(414)
501-
plt.plot(taxis, evstream.stp[0].data, 'k', lw=0.5)
502-
plt.gca().ticklabel_format(axis='y', style='sci', useOffset=True,
503-
scilimits=(-3, 3))
504-
plt.xlim((0., 7200.))
505-
plt.title(evstream.tstamp+': P', fontdict={'fontsize': 8})
476+
fig = plt.figure(figsize=(6, 6))
477+
478+
ax = fig.add_subplot(4, 1, 1)
479+
ax.plot(taxis, evstream.sth.select(component='Z')[0].data, 'k', lw=0.5)
480+
ax.set_title(evstream.key+' '+evstream.tstamp +
481+
': Z', fontdict={'fontsize': 8})
482+
ax.ticklabel_format(axis='y', style='sci', useOffset=True,
483+
scilimits=(-3, 3))
484+
ax.set_xlim((0., 7200.))
485+
486+
if len(evstream.sth) > 1:
487+
ax = fig.add_subplot(4, 1, 2)
488+
ax.plot(taxis, evstream.sth.select(component='1')[0].data, 'k', lw=0.5)
489+
ax.set_xlim((0., 7200.))
490+
ax.set_title(evstream.tstamp+': 1', fontdict={'fontsize': 8})
491+
ax.ticklabel_format(axis='y', style='sci', useOffset=True,
492+
scilimits=(-3, 3))
493+
494+
ax = fig.add_subplot(4, 1, 3)
495+
ax.plot(taxis, evstream.sth.select(component='2')[0].data, 'k', lw=0.5)
496+
ax.set_xlim((0., 7200.))
497+
ax.set_title(evstream.tstamp+': 2', fontdict={'fontsize': 8})
498+
ax.ticklabel_format(axis='y', style='sci', useOffset=True,
499+
scilimits=(-3, 3))
500+
501+
if evstream.stp:
502+
if len(evstream.sth) > 1:
503+
ax = fig.add_subplot(4, 1, 4)
504+
else:
505+
ax = fig.add_subplot(4, 1, 2)
506+
ax.plot(taxis, evstream.stp[0].data, 'k', lw=0.5)
507+
ax.ticklabel_format(axis='y', style='sci', useOffset=True,
508+
scilimits=(-3, 3))
509+
ax.set_xlim((0., 7200.))
510+
ax.set_title(evstream.tstamp+': P', fontdict={'fontsize': 8})
506511

507512
plt.xlabel('Time since earthquake (sec)')
508513
plt.tight_layout()
@@ -535,7 +540,8 @@ def fig_event_corrected(evstream, TF_list):
535540
plt.figure(figsize=(8, 8))
536541

537542
plt.subplot(611)
538-
plt.plot(taxis, evstream.sth[2].data, 'lightgray', lw=0.5)
543+
plt.plot(
544+
taxis, evstream.sth.select(component='Z')[0].data, 'lightgray', lw=0.5)
539545
if TF_list['Z1']:
540546
plt.plot(taxis, evstream.correct['Z1'], 'k', lw=0.5)
541547
plt.title(evstream.key+' '+evstream.tstamp +
@@ -545,7 +551,8 @@ def fig_event_corrected(evstream, TF_list):
545551
plt.xlim((0., 7200.))
546552

547553
plt.subplot(612)
548-
plt.plot(taxis, evstream.sth[2].data, 'lightgray', lw=0.5)
554+
plt.plot(
555+
taxis, evstream.sth.select(component='Z')[0].data, 'lightgray', lw=0.5)
549556
if TF_list['Z2-1']:
550557
plt.plot(taxis, evstream.correct['Z2-1'], 'k', lw=0.5)
551558
plt.title(evstream.tstamp+': Z2-1', fontdict={'fontsize': 8})
@@ -554,7 +561,8 @@ def fig_event_corrected(evstream, TF_list):
554561
plt.xlim((0., 7200.))
555562

556563
plt.subplot(613)
557-
plt.plot(taxis, evstream.sth[2].data, 'lightgray', lw=0.5)
564+
plt.plot(
565+
taxis, evstream.sth.select(component='Z')[0].data, 'lightgray', lw=0.5)
558566
if TF_list['ZP-21']:
559567
plt.plot(taxis, evstream.correct['ZP-21'], 'k', lw=0.5)
560568
plt.title(evstream.tstamp+': ZP-21', fontdict={'fontsize': 8})
@@ -563,7 +571,8 @@ def fig_event_corrected(evstream, TF_list):
563571
plt.xlim((0., 7200.))
564572

565573
plt.subplot(614)
566-
plt.plot(taxis, evstream.sth[2].data, 'lightgray', lw=0.5)
574+
plt.plot(
575+
taxis, evstream.sth.select(component='Z')[0].data, 'lightgray', lw=0.5)
567576
if TF_list['ZH']:
568577
plt.plot(taxis, evstream.correct['ZH'], 'k', lw=0.5)
569578
plt.title(evstream.tstamp+': ZH', fontdict={'fontsize': 8})
@@ -572,7 +581,8 @@ def fig_event_corrected(evstream, TF_list):
572581
plt.xlim((0., 7200.))
573582

574583
plt.subplot(615)
575-
plt.plot(taxis, evstream.sth[2].data, 'lightgray', lw=0.5)
584+
plt.plot(
585+
taxis, evstream.sth.select(component='Z')[0].data, 'lightgray', lw=0.5)
576586
if TF_list['ZP-H']:
577587
plt.plot(taxis, evstream.correct['ZP-H'], 'k', lw=0.5)
578588
plt.title(evstream.tstamp+': ZP-H', fontdict={'fontsize': 8})
@@ -581,7 +591,8 @@ def fig_event_corrected(evstream, TF_list):
581591
plt.xlim((0., 7200.))
582592

583593
plt.subplot(616)
584-
plt.plot(taxis, evstream.sth[2].data, 'lightgray', lw=0.5)
594+
plt.plot(
595+
taxis, evstream.sth.select(component='Z')[0].data, 'lightgray', lw=0.5)
585596
if TF_list['ZP']:
586597
plt.plot(taxis, evstream.correct['ZP'], 'k', lw=0.5)
587598
plt.title(evstream.tstamp+': ZP', fontdict={'fontsize': 8})

obstools/scripts/atacr_clean_spectra.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,7 @@ def main(args=None):
386386
# Load file if it exists
387387
if filespec.exists():
388388
print()
389-
print(
390-
"*******************************************" +
391-
"*****************")
389+
print("*"*60)
392390
print('* Calculating noise spectra for key ' +
393391
stkey+' and day '+year+'.'+jday)
394392
print("* -> file "+str(filespec)+" found - loading")
@@ -543,7 +541,7 @@ def main(args=None):
543541
fname = stkey + '.' + 'av_admittance'
544542
plot = plotting.fig_av_cross(stanoise.f, ad, stanoise.gooddays,
545543
'Admittance', stanoise.ncomp, key=stkey, lw=0.5)
546-
# if plotpath.is_dir():
544+
547545
if plotpath:
548546
plot.savefig(str(plotpath / (fname + '.' + args.form)),
549547
dpi=300, bbox_inches='tight', format=args.form)
@@ -553,14 +551,14 @@ def main(args=None):
553551
fname = stkey + '.' + 'av_phase'
554552
plot = plotting.fig_av_cross(stanoise.f, ph, stanoise.gooddays,
555553
'Phase', stanoise.ncomp, key=stkey, marker=',', lw=0)
556-
# if plotpath.is_dir():
554+
557555
if plotpath:
558556
plot.savefig(str(plotpath / (fname + '.' + args.form)),
559557
dpi=300, bbox_inches='tight', format=args.form)
560558
else:
561559
plot.show()
562560

563-
if args.fig_coh_ph and stanoise.direc.any():
561+
if args.fig_coh_ph and stanoise.direc is not None:
564562
fname = stkey + '.' + 'coh_ph'
565563
plot = plotting.fig_coh_ph(coh_all, ph_all, stanoise.direc)
566564
if plotpath:

obstools/scripts/atacr_correct_event.py

+25-4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ def get_correct_arguments(argv=None):
8585
default=False,
8686
help="Force the overwriting of pre-existing data. " +
8787
"[Default False]")
88+
parser.add_argument(
89+
"-S", "--save",
90+
action="store_true",
91+
dest="save_correct",
92+
default=False,
93+
help="Save the corrected EvStream objects to disk. " +
94+
"[Default False]")
8895

8996
# Event Selection Criteria
9097
DaysGroup = parser.add_argument_group(
@@ -367,8 +374,9 @@ def main(args=None):
367374

368375
if args.fig_event_raw:
369376
fname = stkey + '.' + evstamp + 'raw'
370-
plot = plotting.fig_event_raw(eventstream,
371-
fmin=args.fmin, fmax=args.fmax)
377+
plot = plotting.fig_event_raw(
378+
eventstream,
379+
fmin=args.fmin, fmax=args.fmax)
372380

373381
if plotpath:
374382
plot.savefig(
@@ -413,7 +421,7 @@ def main(args=None):
413421
# average files
414422
eventstream.correct_data(tfaverage)
415423

416-
correct = eventstream.correct
424+
correct_sta = eventstream.correct
417425
if args.fig_plot_corrected:
418426
fname = stkey + '.' + evstamp + 'sta_corrected'
419427
plot = plotting.fig_event_corrected(
@@ -427,6 +435,13 @@ def main(args=None):
427435
else:
428436
plot.show()
429437

438+
if args.save_correct:
439+
correctpath = eventpath / 'CORRECTED'
440+
if not correctpath.is_dir():
441+
correctpath.mkdir(parents=True)
442+
file = correctpath / eventfile.stem
443+
eventstream.save(str(file) + '.day.pkl')
444+
430445
# This case refers to the "daily" spectral averages
431446
else:
432447
if not args.skip_daily:
@@ -447,7 +462,7 @@ def main(args=None):
447462
# average files
448463
eventstream.correct_data(tfaverage)
449464

450-
correct = eventstream.correct
465+
correct_day = eventstream.correct
451466
if args.fig_plot_corrected:
452467
fname = stkey + '.' + evstamp + 'day_corrected'
453468
plot = plotting.fig_event_corrected(
@@ -461,6 +476,12 @@ def main(args=None):
461476
else:
462477
plot.show()
463478

479+
if args.save_correct:
480+
correctpath = eventpath / 'CORRECTED'
481+
if not correctpath.is_dir():
482+
correctpath.mkdir(parents=True)
483+
file = correctpath / eventfile.stem
484+
eventstream.save(str(file) + '.sta.pkl')
464485

465486
if __name__ == "__main__":
466487

obstools/scripts/atacr_daily_spectra.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,7 @@ def main(args=None):
404404
jday = str(trZ.stats.starttime.julday).zfill(3)
405405

406406
print()
407-
print(
408-
"************************************************************")
407+
print("*"*60)
409408
print("* Calculating noise spectra for key " +
410409
stkey+" and day "+year+"."+jday)
411410
tstamp = year+'.'+jday+'.'
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import stdb
2+
import numpy as np
3+
import shutil
4+
from pathlib import Path
5+
from pkg_resources import resource_filename
6+
from obspy.clients.fdsn import Client
7+
from obstools.atacr import DayNoise, StaNoise, TFNoise
8+
from obstools.atacr import EventStream, Power, Cross, Rotation
9+
from obstools.atacr import utils, plotting
10+
11+
dbfile = resource_filename('obstools',
12+
'examples/meta/M08A.pkl')
13+
14+
curdir = Path.cwd()
15+
datadir = curdir / 'DATA'
16+
avgdir = curdir / 'AVG_STA'
17+
cmpdir = curdir / 'COMPL_STA'
18+
evdir = curdir / 'EVENTS'
19+
specdir = curdir / 'SPECTRA'
20+
tfdir = curdir / 'TF_STA'
21+
22+
23+
# Test with pressure only (no H)
24+
def test_01_data_noH():
25+
from obstools.scripts import atacr_download_data as atacr
26+
args0 = atacr.get_daylong_arguments([
27+
dbfile, '--keys', '7D.M08A', '-O',
28+
'--start', '2012-03-08', '--end', '2012-03-10',
29+
'--sampling-rate', '0.5', '--channels', 'P'])
30+
atacr.main(args=args0)
31+
def test_02_daily_noH():
32+
from obstools.scripts import atacr_daily_spectra as atacr
33+
args0 = atacr.get_dailyspec_arguments([
34+
dbfile, '--keys', '7D.M08A', '-O', '--figQC',
35+
'--figAverage', '--save-fig'])
36+
atacr.main(args=args0)
37+
def test_03_clean_noH():
38+
from obstools.scripts import atacr_clean_spectra as atacr
39+
args0 = atacr.get_cleanspec_arguments([
40+
dbfile, '--keys', '7D.M08A', '-O', '--save-fig',
41+
'--figCross', '--figCoh'])
42+
atacr.main(args=args0)
43+
def test_04_trans_noH():
44+
from obstools.scripts import atacr_transfer_functions as atacr
45+
args0 = atacr.get_transfer_arguments([
46+
dbfile, '--keys', '7D.M08A', '-O', '--save-fig', '--figTF'])
47+
atacr.main(args=args0)
48+
def test_05_event_noH():
49+
from obstools.scripts import atacr_download_event as atacr
50+
args0 = atacr.get_event_arguments([
51+
dbfile, '--keys', '7D.M08A', '-O',
52+
'--start', '2012-03-08', '--end', '2012-03-10',
53+
'--min-mag', '6.3', '--max-mag', '6.7',
54+
'--sampling-rate', '0.5', '--channels', 'P'])
55+
atacr.main(args=args0)
56+
def test_06_correct_noH():
57+
from obstools.scripts import atacr_correct_event as atacr
58+
args0 = atacr.get_correct_arguments([
59+
dbfile, '--keys', '7D.M08A', '-O', '--figRaw',
60+
'--figClean', '--save-fig', '--save'])
61+
atacr.main(args=args0)
62+
def test_07_comply_noH():
63+
from obstools.scripts import comply_calculate as comply
64+
args0 = comply.get_comply_arguments([
65+
dbfile, '--keys', '7D.M08A', '-O', '--save-fig', '--fig'])
66+
comply.main(args=args0)
67+
68+
def test_08_rmtree():
69+
shutil.rmtree(datadir)
70+
shutil.rmtree(avgdir)
71+
shutil.rmtree(evdir)
72+
shutil.rmtree(specdir)
73+
shutil.rmtree(tfdir)
74+
shutil.rmtree(cmpdir)

0 commit comments

Comments
 (0)