Skip to content

Commit

Permalink
Add tests for deprecated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tclements committed May 27, 2020
1 parent 957b956 commit da11959
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export compute_fft, compute_cc

@deprecate compute_fft() rfft()
@deprecate compute_cc() correlate()

"""
compute_fft(R)
Expand Down Expand Up @@ -37,13 +40,13 @@ Cross-correlation can be done using one of three options:
`deconv`.
"""
function compute_cc(FFT1::FFTData, FFT2::FFTData, maxlag::Float64;
corr_type::String="cross-correlation")
corr_type::String="CC")

comp = FFT1.name[end] * FFT2.name[end]
# get intersect of dates; return nothing if no intersect
inter = intersect(FFT1.t,FFT2.t)
if length(inter) == 0
return nothing
throw(ArgumentError("No common windows for $(FFT1.name)-$(FFT2.name) $(FFT1.id)"))
end

N = convert(Int,round(FFT1.cc_len * FFT1.fs)) # number of data points
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ include("test_rotation.jl")
include("test_plotting.jl")
#
# # out with the old
# include("test_deprecated.jl")
include("test_deprecated.jl")
73 changes: 73 additions & 0 deletions test/test_deprecated.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# test deprecated functions

T = Float32
fs = 100. # sampling rate
N = 2^14 # number of points
Nwin = 10
freqmin = 10. # minimum frequency
freqmax = 20. # maximum frequency
corners = 4 # number of corners
zerophase = true # do zerophase filtering
maxlag = 20. # maximum lag time in coda
starttime = d2u(DateTime(Date(now()))) # starttime for SeisChanel / SeisData


# test FFTData creation with compute_fft
@testset "compute fft" begin

cc_len = 10.24
cc_step = 5.12
C = SeisChannel()
C.x = rand(T,N)
C.fs = fs
C.id = "MZ.KTFD.08.ENZ"
C.t = [1 (starttime + 0.004) * 1e6;N 0]
R = RawData(C,cc_len,cc_step)
R.gain = 1.234234e6
F = compute_fft(R)
@test F.name == C.id == R.name
@test F.id == R.id
@test F.loc == R.loc
@test F.fs == R.fs
@test F.gain == R.gain
@test F.freqmin == R.freqmin
@test F.freqmax == F.freqmax
@test F.cc_len == R.cc_len == cc_len
@test F.cc_step == R.cc_step == cc_step
@test F.whitened == R.whitened
@test F.time_norm == R.time_norm
@test F.resp == R.resp
@test F.t == R.t
@test length(F.t) == size(F.fft,2)
end

# test correlation with FFTData
@testset "compute cc" begin
cc_len = 40.96 # length of noise window
cc_step = 40.96 # step between windows
Ch = SeisIO.RandSeis.randSeisChannel(c=false,s=true)
ungap!(Ch)
Ch.x = rand(T,Int(cc_len*Nwin*fs +1)) .- T(0.5)
Ch.fs = fs
Ch.t = [1 starttime * 1e6;length(Ch.x) 0]
Ch.loc = GeoLoc()
R = RawData(Ch,cc_len,cc_step)
F = rfft(R)
C = compute_cc(F,F,maxlag)
@test isa(C,CorrData) # test return type
@test C.comp == repeat(Ch.id[end],2) # test component name
@test size(C.corr) == (Int(2 * maxlag * fs) + 1,Nwin) # test size
@test eltype(C.corr) == eltype(Ch.x) # test type stability
t = -maxlag:1/fs:maxlag
maxinds = argmax(C.corr,dims=1)
@test all([t[maxinds[ii][1]] == 0 for ii in length(maxinds)]) # test max args
@test C.rotated == false # test rotation
@test C.corr_type == "CC" # test cross-correlation

# test with no intersecting windows
F1 = deepcopy(F)
F2 = deepcopy(F)
F2.t = rand(eltype(F2.t),size(F2.fft,2))
@test_throws ArgumentError compute_cc(F1,F2,maxlag)

end

0 comments on commit da11959

Please sign in to comment.