Skip to content

Commit

Permalink
fixed sorting issue related to #144 and extended test to cover this f…
Browse files Browse the repository at this point in the history
…unction
  • Loading branch information
bch0w committed May 16, 2024
1 parent 500341d commit 49eed72
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
29 changes: 28 additions & 1 deletion pysep/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from obspy import read, read_events, read_inventory, Stream
from obspy.io.sac.sactrace import SACTrace
from pysep.utils.cap_sac import (append_sac_headers,
format_sac_header_w_taup_traveltimes)
format_sac_header_w_taup_traveltimes,
write_cap_weights_files)
from pysep.utils.curtail import (remove_for_clipped_amplitudes, rename_channels,
remove_stations_for_missing_channels,
remove_stations_for_insufficient_length,
Expand Down Expand Up @@ -101,6 +102,32 @@ def test_sac_header_correct_origin_time(tmpdir, test_st, test_inv, test_event):
assert(sac.reftime == test_event.preferred_origin().time)


def test_write_cap_weights_files(tmpdir, test_st, test_inv, test_event):
"""
Test writing out CAP weight files and make sure sorting works for distance
and code works as advertised. No testing for az sorting
"""
st = append_sac_headers(st=test_st, inv=test_inv, event=test_event)

# Check sorting by dist
write_cap_weights_files(st=st, path_out=tmpdir, order_by="dist")
assert(os.path.exists(os.path.join(tmpdir, "weights.dat")))
dists = np.loadtxt(os.path.join(tmpdir, "weights.dat"), usecols=1)
# Check that distances are in ascending order
assert(np.all(np.diff(dists) >= 0))

# Remove so we know that new files are being made each time
os.remove(os.path.join(tmpdir, "weights.dat"))

# Check sorting by code
write_cap_weights_files(st=st, path_out=tmpdir, order_by="code")
assert(os.path.exists(os.path.join(tmpdir, "weights.dat")))
codes = np.loadtxt(os.path.join(tmpdir, "weights.dat"), usecols=0,
dtype=str)
# Check that distances are in ascending order
assert(np.all(codes == np.sort(codes)))


def test_rename_channels(test_st):
"""
Edit some waveforms to be obviously bad and make sure we can catch it
Expand Down
2 changes: 1 addition & 1 deletion pysep/utils/cap_sac.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def write_cap_weights_files(st, path_out="./", order_by="dist"):

# Order codes based on distance, name or azimuth
idx = ["code", "dist", "az"].index(order_by)
code_list = np.array(code_list)
code_list = np.array(code_list, dtype="object")
ordered_codes = code_list[code_list[:, idx].argsort()]

logger.info("writing CAP weight files")
Expand Down

0 comments on commit 49eed72

Please sign in to comment.