Skip to content

Commit

Permalink
Merge pull request #8 from anowacki/channel-code-parts-warning
Browse files Browse the repository at this point in the history
Return documented things in channel_code_parts and don't throw errors
  • Loading branch information
anowacki authored Feb 26, 2024
2 parents cda0fcc + b396060 commit 4ec945f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 35 deletions.
20 changes: 13 additions & 7 deletions src/channel_codes.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Utilities for dealing with channel codes/trace IDs

"""
channel_code_parts(s) (net, sta, loc, cha)
channel_code_parts(s) -> (net, sta, loc, cha)
Convert a single `String` `s` into its component SEED channel code parts.
Returns a named tuple of network `net`, station `sta`, location `loc` and
channel `cha`. Empty components are given as empty `String`s.
If the channel code does not appear to match any known pattern, `s` is
returned in `sta` and all other parts are empty.
This function assumes that `s` is an ASCII string, as per the SEED convention
for channel IDs.
Expand All @@ -28,17 +31,20 @@ function channel_code_parts(s::String)
cha = join(parts[4:end])
# Traditional SEED convention: NET_STA_LOC_CHA
elseif nparts == 4
length(parts[1]) > 6 || error("unexpectedly short network name")
net = parts[1][1] == 'X' ? parts[1][7:end] : parts[1][6:end]
sta = parts[2]
loc = parts[3]
cha = parts[4]
else
error("unexpected apparent XFDSN URN")
@warn("unexpected apparent XFDSN URN")
net = loc = cha = ""
sta = s
end
# Not really an XFDSN URN
else
error("unexpectedly short channel id")
@warn("unexpectedly short channel id")
net = loc = cha = ""
sta = s
end

# Not an XFDSN URN but might have the same structure
Expand All @@ -57,10 +63,10 @@ function channel_code_parts(s::String)
cha = join(parts[4:end])
# Something else entirely: just put it all in sta
else
net = nothing
net = ""
sta = s
loc = nothing
cha = nothing
loc = ""
cha = ""
end
end
(net=net, sta=sta, loc=loc, cha=cha)
Expand Down
104 changes: 76 additions & 28 deletions test/channel_codes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,81 @@ using Test
@testset "Channel codes" begin
@testset "channel_code_parts" begin
@testset "FDSN format" begin
# New format: separate channel code elements
@test LibMseed.channel_code_parts("XFDSN:AA_BB_CC_D_E_F") ==
(net="AA", sta="BB", loc="CC", cha="DEF")
@test LibMseed.channel_code_parts("FDSN:AA_BB_CC_D_E_F") ==
(net="AA", sta="BB", loc="CC", cha="DEF")
# Old format: single channel code
@test LibMseed.channel_code_parts("XFDSN:AA_BB_CC_DEF") ==
(net="AA", sta="BB", loc="CC", cha="DEF")
@test LibMseed.channel_code_parts("FDSN:AA_BB_CC_DEF") ==
(net="AA", sta="BB", loc="CC", cha="DEF")
# No _s
@test_throws ErrorException LibMseed.channel_code_parts("XFDSN:ldfjaldjjlfjk")
@test_throws ErrorException LibMseed.channel_code_parts("FDSN:ldfjaldjjlfjk")
# Too many parts
@test_throws ErrorException LibMseed.channel_code_parts("XFDSN:A_B_C_D_E_F_G")
@test_throws ErrorException LibMseed.channel_code_parts("FDSN:A_B_C_D_E_F_G")
# Not enough parts
@test_throws ErrorException LibMseed.channel_code_parts("XFDSN:A_B_C_D_E")
@test_throws ErrorException LibMseed.channel_code_parts("FDSN:A_B_C_D_E")
# Empty network code
@test LibMseed.channel_code_parts("FDSN:_BB_CC_D_E_F") ==
(net="", sta="BB", loc="CC", cha="DEF")
@test LibMseed.channel_code_parts("XFDSN:_BB_CC_D_E_F") ==
(net="", sta="BB", loc="CC", cha="DEF")
# 'Empty' everything
@test LibMseed.channel_code_parts("XFDSN:___ _ _ ") ==
(net="", sta="", loc="", cha=" ")
@testset "New format: separate channel code elements" begin
@test LibMseed.channel_code_parts("XFDSN:AA_BB_CC_D_E_F") ==
(net="AA", sta="BB", loc="CC", cha="DEF")
@test LibMseed.channel_code_parts("FDSN:AA_BB_CC_D_E_F") ==
(net="AA", sta="BB", loc="CC", cha="DEF")
# Old format: single channel code
@test LibMseed.channel_code_parts("XFDSN:AA_BB_CC_DEF") ==
(net="AA", sta="BB", loc="CC", cha="DEF")
@test LibMseed.channel_code_parts("FDSN:AA_BB_CC_DEF") ==
(net="AA", sta="BB", loc="CC", cha="DEF")
end

@testset "No _s" begin
@test (
@test_logs (
:warn, "unexpected apparent XFDSN URN"
) LibMseed.channel_code_parts("XFDSN:ldfjaldjjlfjk")
) == (net="", sta="XFDSN:ldfjaldjjlfjk", loc="", cha="")
@test (
@test_logs (
:warn, "unexpected apparent XFDSN URN"
) LibMseed.channel_code_parts("FDSN:ldfjaldjjlfjk")
) == (net="", sta="FDSN:ldfjaldjjlfjk", loc="", cha="")
end

@testset "Too many parts" begin
@test (
@test_logs (
:warn, "unexpected apparent XFDSN URN"
) LibMseed.channel_code_parts("XFDSN:A_B_C_D_E_F_G")
) == (net="", sta="XFDSN:A_B_C_D_E_F_G", loc="", cha="")
@test (
@test_logs (
:warn, "unexpected apparent XFDSN URN"
) LibMseed.channel_code_parts("FDSN:A_B_C_D_E_F_G")
) == (net="", sta="FDSN:A_B_C_D_E_F_G", loc="", cha="")
end

@testset "Not enough parts" begin
@test (
@test_logs (
:warn, "unexpected apparent XFDSN URN"
) LibMseed.channel_code_parts("XFDSN:A_B_C_D_E")
) == (net="", sta="XFDSN:A_B_C_D_E", loc="", cha="")
@test (
@test_logs (
:warn, "unexpected apparent XFDSN URN"
) LibMseed.channel_code_parts("FDSN:A_B_C_D_E")
) == (net="", sta="FDSN:A_B_C_D_E", loc="", cha="")
end

@testset "Empty network code" begin
@test LibMseed.channel_code_parts("FDSN:_BB_CC_D_E_F") ==
(net="", sta="BB", loc="CC", cha="DEF")
@test LibMseed.channel_code_parts("XFDSN:_BB_CC_D_E_F") ==
(net="", sta="BB", loc="CC", cha="DEF")
end

@testset "'Empty' everything" begin
@test LibMseed.channel_code_parts("XFDSN:___ _ _ ") ==
(net="", sta="", loc="", cha=" ")
end

@testset "Empty after '[X]FDSN:'" begin
@test (
@test_logs (
:warn, "unexpectedly short channel id"
) LibMseed.channel_code_parts("XFDSN:")
) == (net="", sta="XFDSN:", loc="", cha="")
@test (
@test_logs (
:warn, "unexpectedly short channel id"
) LibMseed.channel_code_parts("FDSN:")
) == (net="", sta="FDSN:", loc="", cha="")
end
end

@testset "_ separated" begin
Expand All @@ -45,7 +93,7 @@ using Test

@testset "Other format" begin
@test LibMseed.channel_code_parts("AABBCCDEF") ==
(net=nothing, sta="AABBCCDEF", loc=nothing, cha=nothing)
(net="", sta="AABBCCDEF", loc="", cha="")
end

@testset "MseedTraceID" begin
Expand Down

0 comments on commit 4ec945f

Please sign in to comment.