Skip to content

Commit

Permalink
fix interpol logic, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
juliasloan25 committed Aug 3, 2023
1 parent 3ec2921 commit de3a7e1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/BCReader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ or returns the first Field if interpolation is switched off.
- Fields.field
"""
function interpolate_midmonth_to_daily(date, bcf_info::BCFileInfo{FT}) where {FT}
if bcf_info.interpolate_daily && bcf_info.segment_length[1] > FT(0)
(; segment_length, segment_idx, all_dates, monthly_fields) = bcf_info

(; segment_length, segment_idx, all_dates, monthly_fields) = bcf_info
if bcf_info.interpolate_daily && bcf_info.segment_length[1] > FT(0) &&
date != all_dates[Int(segment_idx[1])]
return interpol.(
monthly_fields[1],
monthly_fields[2],
Expand Down Expand Up @@ -306,7 +306,7 @@ function interpol(f1::FT, f2::FT, Δt_tt1::FT, Δt_t2t1::FT) where {FT}
@assert Δt_t2t1 > FT(0) "t2 must be > t1, but `Δt_t2t1` = $Δt_t2t1"
interp_fraction = Δt_tt1 / Δt_t2t1
@assert abs(interp_fraction) <= FT(1) "time interpolation weights must be <= 1, but `interp_fraction` = $interp_fraction"
return f1 * interp_fraction + f2 * (FT(1) - interp_fraction)
return f1 * (FT(1) - interp_fraction) + f2 * interp_fraction
end

end
21 changes: 21 additions & 0 deletions test/bcreader_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ for FT in (Float32, Float64)
end
end

@testset "test interpol for FT=$FT" begin
# Setup
t1 = FT(0)
t2 = FT(10)

f1 = FT(-1)
f2 = FT(1)

# Case 1: t1 < t < t2 --> should return average (0)
t = FT(5)
@test BCReader.interpol(f1, f2, t - t1, t2 - t1) == 0

# Case 2: t1 = t < t2 --> should return val at t1 (f1)
t = FT(0)
@test BCReader.interpol(f1, f2, t - t1, t2 - t1) == f1

# Case 3: t1 < t = t2 --> should return val at t2 (f2)
t = FT(10)
@test BCReader.interpol(f1, f2, t - t1, t2 - t1) == f2
end

@testset "test interpolate_midmonth_to_daily for FT=$FT" begin
# test interpolate_midmonth_to_daily with interpolation
interpolate_daily = true
Expand Down

0 comments on commit de3a7e1

Please sign in to comment.