Skip to content

Commit 7017cc4

Browse files
authored
Merge pull request #36 from nfsi-canada/scripts
Script verbosity, unit tests, windowed fft
2 parents f4ee834 + 8836e42 commit 7017cc4

14 files changed

+369
-364
lines changed

Diff for: .github/workflows/deploy.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,21 @@ on:
1717
jobs:
1818
# This workflow contains a single job called "build"
1919
build:
20+
name: Python ${{ matrix.python-version }}
2021
# The type of runner that the job will run on
2122
runs-on: ${{ matrix.os }}
2223
strategy:
2324
matrix:
2425
os: [ubuntu-latest]
25-
python-version: [3.7, 3.8, 3.9]
26+
python-version: ['3.7', '3.8', '3.9']
2627

2728
steps:
2829
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
2930
- uses: actions/checkout@v2
3031
- uses: conda-incubator/setup-miniconda@v2
3132
with:
33+
auto-update-conda: true
34+
python-version: ${{ matrix.python-version }}
3235
activate-environment: test
3336
auto-activate-base: false
3437

@@ -44,6 +47,7 @@ jobs:
4447
- name: Tests
4548
shell: bash -l {0}
4649
run: |
50+
export MPLBACKEND=Agg
4751
mkdir empty
4852
cd empty
4953
conda install pytest-cov

Diff for: docs/atacr.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ M08A and send the prompt to a logfile
731731

732732
.. code-block::
733733
734-
$ query_fdsn_stdb.py -N 7D -C ?H? -S M08A M08A > logfile
734+
$ query_fdsn_stdb -N 7D -C ?H? -S M08A M08A > logfile
735735
736736
.. note::
737737

@@ -743,11 +743,11 @@ M08A and send the prompt to a logfile
743743
enclose the `?H?` in single or double quotes (e.g.,
744744
`query_fdsn_stdb.py -N 7D -C "?H?" -S M08A M08A > logfile`)
745745

746-
To check the station info for M08A, use the program ``ls_stdb.py``:
746+
To check the station info for M08A, use the program ``ls_stdb``:
747747

748748
.. code-block::
749749
750-
$ ls_stdb.py M08A.pkl
750+
$ ls_stdb M08A.pkl
751751
Listing Station Pickle: M08A.pkl
752752
7D.M08A
753753
--------------------------------------------------------------------------
@@ -1144,7 +1144,7 @@ Vanuatu earthquake (be conservative with the options), type in a terminal:
11441144
| Found 1 possible events |
11451145
11461146
****************************************************
1147-
* #1 (2/1): 20120309_070953
1147+
* (1/1): 20120309_070953
11481148
* Origin Time: 2012-03-09 07:09:53
11491149
* Lat: -19.22; Lon: 169.75
11501150
* Dep: 33.70; Mag: 6.6

Diff for: obstools/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
110110
"""
111111

112-
__version__ = '0.1.2'
112+
__version__ = '0.1.3'
113113

114114
__author__ = 'Pascal Audet & Helen Janiszewski'
115115

Diff for: obstools/atacr/classes.py

+50-28
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323

2424
import sys
25-
from scipy.signal import spectrogram, detrend
25+
from scipy.signal import spectrogram, stft, detrend
2626
from scipy.linalg import norm
2727
import matplotlib.pyplot as plt
2828
import numpy as np
@@ -349,6 +349,8 @@ def QC_daily_spectra(self, pd=[0.004, 0.2], tol=1.5, alpha=0.05,
349349
psdP = None
350350
f, t, psdZ = spectrogram(
351351
self.trZ.data, self.fs, window=wind, nperseg=ws, noverlap=ss)
352+
self.f = f
353+
print(f)
352354
if self.ncomp == 2 or self.ncomp == 4:
353355
f, t, psdP = spectrogram(
354356
self.trP.data, self.fs, window=wind, nperseg=ws, noverlap=ss)
@@ -640,20 +642,37 @@ def average_daily_spectra(self, calc_rotation=True, fig_average=False,
640642
ws = int(self.window/self.dt)
641643

642644
# Number of points in step
643-
ss = int(self.window*(1.-self.overlap)/self.dt)
645+
ss = int(self.window*self.overlap/self.dt)
646+
647+
# hanning window
648+
hanning = np.hanning(2*ss)
649+
wind = np.ones(ws)
650+
wind[0:ss] = hanning[0:ss]
651+
wind[-ss:ws] = hanning[ss:ws]
644652

645653
ft1 = None
646654
ft2 = None
647655
ftZ = None
648656
ftP = None
649-
ftZ, f = utils.calculate_windowed_fft(self.trZ, ws, ss)
657+
658+
_f, _t, ftZ = stft(
659+
self.trZ.data, self.fs, return_onesided=False, boundary=None, padded=False,
660+
window=wind, nperseg=ws, noverlap=ss)
661+
ftZ = ftZ.T
650662
if self.ncomp == 2 or self.ncomp == 4:
651-
ftP, f = utils.calculate_windowed_fft(self.trP, ws, ss)
663+
_f, _t, ftP = stft(
664+
self.trP.data, self.fs, return_onesided=False, boundary=None, padded=False,
665+
window=wind, nperseg=ws, noverlap=ss)
666+
ftP = ftP.T
652667
if self.ncomp == 3 or self.ncomp == 4:
653-
ft1, f = utils.calculate_windowed_fft(self.tr1, ws, ss)
654-
ft2, f = utils.calculate_windowed_fft(self.tr2, ws, ss)
655-
656-
self.f = f
668+
_f, _t, ft1 = stft(
669+
self.tr1.data, self.fs, return_onesided=False, boundary=None, padded=False,
670+
window=wind, nperseg=ws, noverlap=ss)
671+
_f, _t, ft2 = stft(
672+
self.tr2.data, self.fs, return_onesided=False, boundary=None, padded=False,
673+
window=wind, nperseg=ws, noverlap=ss)
674+
ft1 = ft1.T
675+
ft2 = ft2.T
657676

658677
# Extract good windows
659678
c11 = None
@@ -662,18 +681,18 @@ def average_daily_spectra(self, calc_rotation=True, fig_average=False,
662681
cPP = None
663682
cZZ = np.abs(
664683
np.mean(ftZ[self.goodwins, :]*np.conj(ftZ[self.goodwins, :]),
665-
axis=0))[0:len(f)]
684+
axis=0))[0:len(self.f)]
666685
if self.ncomp == 2 or self.ncomp == 4:
667686
cPP = np.abs(
668687
np.mean(ftP[self.goodwins, :]*np.conj(ftP[self.goodwins, :]),
669-
axis=0))[0:len(f)]
688+
axis=0))[0:len(self.f)]
670689
if self.ncomp == 3 or self.ncomp == 4:
671690
c11 = np.abs(
672691
np.mean(ft1[self.goodwins, :]*np.conj(ft1[self.goodwins, :]),
673-
axis=0))[0:len(f)]
692+
axis=0))[0:len(self.f)]
674693
c22 = np.abs(
675694
np.mean(ft2[self.goodwins, :]*np.conj(ft2[self.goodwins, :]),
676-
axis=0))[0:len(f)]
695+
axis=0))[0:len(self.f)]
677696

678697
# Extract bad windows
679698
bc11 = None
@@ -683,18 +702,18 @@ def average_daily_spectra(self, calc_rotation=True, fig_average=False,
683702
if np.sum(~self.goodwins) > 0:
684703
bcZZ = np.abs(np.mean(
685704
ftZ[~self.goodwins, :]*np.conj(ftZ[~self.goodwins, :]),
686-
axis=0))[0:len(f)]
705+
axis=0))[0:len(self.f)]
687706
if self.ncomp == 2 or self.ncomp == 4:
688707
bcPP = np.abs(np.mean(
689708
ftP[~self.goodwins, :]*np.conj(ftP[~self.goodwins, :]),
690-
axis=0))[0:len(f)]
709+
axis=0))[0:len(self.f)]
691710
if self.ncomp == 3 or self.ncomp == 4:
692711
bc11 = np.abs(np.mean(
693712
ft1[~self.goodwins, :]*np.conj(ft1[~self.goodwins, :]),
694-
axis=0))[0:len(f)]
713+
axis=0))[0:len(self.f)]
695714
bc22 = np.abs(np.mean(
696715
ft2[~self.goodwins, :]*np.conj(ft2[~self.goodwins, :]),
697-
axis=0))[0:len(f)]
716+
axis=0))[0:len(self.f)]
698717

699718
# Calculate mean of all good windows if component combinations exist
700719
c12 = None
@@ -705,27 +724,27 @@ def average_daily_spectra(self, calc_rotation=True, fig_average=False,
705724
cZP = None
706725
if self.ncomp == 3 or self.ncomp == 4:
707726
c12 = np.mean(ft1[self.goodwins, :] *
708-
np.conj(ft2[self.goodwins, :]), axis=0)[0:len(f)]
727+
np.conj(ft2[self.goodwins, :]), axis=0)[0:len(self.f)]
709728
c1Z = np.mean(ft1[self.goodwins, :] *
710-
np.conj(ftZ[self.goodwins, :]), axis=0)[0:len(f)]
729+
np.conj(ftZ[self.goodwins, :]), axis=0)[0:len(self.f)]
711730
c2Z = np.mean(ft2[self.goodwins, :] *
712-
np.conj(ftZ[self.goodwins, :]), axis=0)[0:len(f)]
731+
np.conj(ftZ[self.goodwins, :]), axis=0)[0:len(self.f)]
713732
if self.ncomp == 4:
714733
c1P = np.mean(ft1[self.goodwins, :] *
715-
np.conj(ftP[self.goodwins, :]), axis=0)[0:len(f)]
734+
np.conj(ftP[self.goodwins, :]), axis=0)[0:len(self.f)]
716735
c2P = np.mean(ft2[self.goodwins, :] *
717-
np.conj(ftP[self.goodwins, :]), axis=0)[0:len(f)]
736+
np.conj(ftP[self.goodwins, :]), axis=0)[0:len(self.f)]
718737
if self.ncomp == 2 or self.ncomp == 4:
719738
cZP = np.mean(ftZ[self.goodwins, :] *
720-
np.conj(ftP[self.goodwins, :]), axis=0)[0:len(f)]
739+
np.conj(ftP[self.goodwins, :]), axis=0)[0:len(self.f)]
721740

722741
# Store as attributes
723742
self.power = Power(c11, c22, cZZ, cPP)
724743
self.cross = Cross(c12, c1Z, c1P, c2Z, c2P, cZP)
725744
bad = Power(bc11, bc22, bcZZ, bcPP)
726745

727746
if fig_average:
728-
plot = plotting.fig_average(f, self.power, bad, self.goodwins,
747+
plot = plotting.fig_average(self.f, self.power, bad, self.goodwins,
729748
self.ncomp, key=self.key)
730749
if save:
731750
fname = self.key + '.' + self.tkey + '.' + 'average.' + form
@@ -739,7 +758,7 @@ def average_daily_spectra(self, calc_rotation=True, fig_average=False,
739758
if calc_rotation and self.ncomp >= 3:
740759
cHH, cHZ, cHP, coh, ph, direc, tilt, coh_value, phase_value = \
741760
utils.calculate_tilt(
742-
ft1, ft2, ftZ, ftP, f, self.goodwins)
761+
ft1, ft2, ftZ, ftP, self.f, self.goodwins)
743762
self.rotation = Rotation(
744763
cHH, cHZ, cHP, coh, ph, tilt, coh_value, phase_value, direc)
745764

@@ -1949,12 +1968,15 @@ def correct_data(self, tfnoise):
19491968
ft2 = None
19501969
ftZ = None
19511970
ftP = None
1952-
ftZ, f = utils.calculate_windowed_fft(trZ, ws, hann=False)
1971+
ftZ = np.fft.fft(trZ, n=ws)
19531972
if self.ncomp == 2 or self.ncomp == 4:
1954-
ftP, f = utils.calculate_windowed_fft(trP, ws, hann=False)
1973+
ftP = np.fft.fft(trP, n=ws)
19551974
if self.ncomp == 3 or self.ncomp == 4:
1956-
ft1, f = utils.calculate_windowed_fft(tr1, ws, hann=False)
1957-
ft2, f = utils.calculate_windowed_fft(tr2, ws, hann=False)
1975+
ft1 = np.fft.fft(tr1, n=ws)
1976+
ft2 = np.fft.fft(tr2, n=ws)
1977+
1978+
# Use one-sided frequency axis to match spectrogram
1979+
f = np.fft.rfftfreq(ws, d=self.dt)
19581980

19591981
if not np.allclose(f, tfnoise.f):
19601982
raise(Exception('Frequency axes are different: ', f, tfnoise.f))

Diff for: obstools/scripts/atacr_clean_spectra.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,7 @@ def main(args=None):
317317
sta.location = tlocs
318318

319319
# Update Display
320-
print()
321-
print("|===============================================|")
320+
print("\n|===============================================|")
322321
print("|===============================================|")
323322
print("| {0:>8s} |".format(
324323
sta.station))
@@ -385,8 +384,7 @@ def main(args=None):
385384

386385
# Load file if it exists
387386
if filespec.exists():
388-
print()
389-
print("*"*60)
387+
print("\n"+"*"*60)
390388
print('* Calculating noise spectra for key ' +
391389
stkey+' and day '+year+'.'+jday)
392390
print("* -> file "+str(filespec)+" found - loading")

Diff for: obstools/scripts/atacr_daily_spectra.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,13 @@ def main(args=None):
321321
# Path where data are located
322322
datapath = Path('DATA') / stkey
323323
if not datapath.is_dir():
324-
print()
325-
print("Path to "+str(datapath)+" doesn`t exist - continuing")
324+
print("\nPath to "+str(datapath)+" doesn`t exist - continuing")
326325
continue
327326

328327
# Path where spectra will be saved
329328
specpath = Path('SPECTRA') / stkey
330329
if not specpath.is_dir():
331-
print()
332-
print("Path to "+str(specpath)+" doesn`t exist - creating it")
330+
print("\nPath to "+str(specpath)+" doesn`t exist - creating it")
333331
specpath.mkdir(parents=True)
334332

335333
# Path where plots will be saved
@@ -365,8 +363,7 @@ def main(args=None):
365363
sta.location = tlocs
366364

367365
# Update Display
368-
print()
369-
print("|===============================================|")
366+
print("\n|===============================================|")
370367
print("|===============================================|")
371368
print("| {0:>8s} |".format(
372369
sta.station))
@@ -403,8 +400,7 @@ def main(args=None):
403400
year = str(trZ.stats.starttime.year).zfill(4)
404401
jday = str(trZ.stats.starttime.julday).zfill(3)
405402

406-
print()
407-
print("*"*60)
403+
print("\n"+"*"*60)
408404
print("* Calculating noise spectra for key " +
409405
stkey+" and day "+year+"."+jday)
410406
tstamp = year+'.'+jday+'.'

Diff for: obstools/scripts/atacr_download_data.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,7 @@ def main(args=None):
318318
# Define path to see if it exists
319319
datapath = Path('DATA') / Path(stkey)
320320
if not datapath.is_dir():
321-
print()
322-
print('Path to '+str(datapath)+' doesn`t exist - creating it')
321+
print('\nPath to '+str(datapath)+' doesn`t exist - creating it')
323322
datapath.mkdir(parents=True)
324323

325324
# Establish client
@@ -354,8 +353,7 @@ def main(args=None):
354353
sta.location = tlocs
355354

356355
# Update Display
357-
print()
358-
print("|===============================================|")
356+
print("\n|===============================================|")
359357
print("|===============================================|")
360358
print("| {0:>8s} |".format(
361359
sta.station))
@@ -389,9 +387,7 @@ def main(args=None):
389387
# Time stamp
390388
tstamp = str(t1.year).zfill(4)+'.'+str(t1.julday).zfill(3)+'.'
391389

392-
print()
393-
print(
394-
"***********************************************************")
390+
print("\n"+"*"*60)
395391
print("* Downloading day-long data for key "+stkey +
396392
" and day "+str(t1.year)+"."+str(t1.julday))
397393
print("*")

0 commit comments

Comments
 (0)