Skip to content

Commit

Permalink
fix bc, clarify test
Browse files Browse the repository at this point in the history
  • Loading branch information
LenkaNovak committed Jan 29, 2024
1 parent c75f432 commit 5eb309d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
21 changes: 11 additions & 10 deletions src/BCReader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,8 @@ function update_midmonth_data!(date, bcf_info::BCFileInfo{FT}) where {FT}
midmonth_idx = bcf_info.segment_idx[1]
midmonth_idx0 = bcf_info.segment_idx0[1]

# case 1: date is before the first date in the file
if (midmonth_idx == midmonth_idx0) && (Dates.days(date - all_dates[midmonth_idx]) <= 0) # for init
midmonth_idx = bcf_info.segment_idx[1] -= Int(1)
midmonth_idx = midmonth_idx < Int(1) ? midmonth_idx + Int(1) : midmonth_idx
@warn "this time period is before BC data - using file from $(all_dates[midmonth_idx0])"
bcf_info.monthly_fields[1] .= scaling_function(
Regridder.read_from_hdf5(bcfile_dir, hd_outfile_root, all_dates[Int(midmonth_idx0)], varname, comms_ctx),
Expand All @@ -196,8 +195,9 @@ function update_midmonth_data!(date, bcf_info::BCFileInfo{FT}) where {FT}
bcf_info.monthly_fields[2] .= deepcopy(bcf_info.monthly_fields[1])
bcf_info.segment_length .= Int(0)

elseif Dates.days(date - all_dates[end - 1]) > 0 # for fini
@warn "this time period is after BC data - using file from $(all_dates[end - 1])"
# case 2: date is after the last date in the file
elseif Dates.days(date - all_dates[end]) > 0 # for fini
@warn "this time period is after BC data - using file from $(all_dates[end])"
bcf_info.monthly_fields[1] .= scaling_function(
Regridder.read_from_hdf5(
bcfile_dir,
Expand All @@ -211,23 +211,22 @@ function update_midmonth_data!(date, bcf_info::BCFileInfo{FT}) where {FT}
bcf_info.monthly_fields[2] .= deepcopy(bcf_info.monthly_fields[1])
bcf_info.segment_length .= Int(0)

# throw error when there are closer initial indices for the bc file data that matches this date0
elseif Dates.days(date - all_dates[Int(midmonth_idx + 1)]) > 2
# case 3: during initialization, throw error if there are closer file dates to the current date than the file date implied by midmonth_idx0+1
elseif (midmonth_idx == midmonth_idx0) && Dates.days(date - all_dates[Int(midmonth_idx0) + 1]) > 2
nearest_idx = argmin(
abs.(
parse(FT, TimeManager.datetime_to_strdate(date)) .-
parse.(FT, TimeManager.datetime_to_strdate.(all_dates[:]))
),
)
# TODO test this
bcf_info.segment_idx[1] = midmonth_idx = midmonth_idx0 = nearest_idx
bcf_info.segment_idx[1] = bcf_info.segment_idx0[1] = nearest_idx
@warn "init data does not correspond to start date. Initializing with `SIC_info.segment_idx[1] = midmonth_idx = midmonth_idx0 = $nearest_idx` for this start date"

# date crosses to the next month
# case 4: date crosses to the next month
elseif Dates.days(date - all_dates[Int(midmonth_idx)]) > 0
midmonth_idx = bcf_info.segment_idx[1] += Int(1)
@warn "On $date updating monthly data files: mid-month dates = [ $(all_dates[Int(midmonth_idx)]) , $(all_dates[Int(midmonth_idx+1)]) ]"
bcf_info.segment_length .= (all_dates[Int(midmonth_idx + 1)] - all_dates[Int(midmonth_idx)]).value
bcf_info.segment_length .= (all_dates[Int(midmonth_idx + 1)] - all_dates[Int(midmonth_idx)]).value # milliseconds!
bcf_info.monthly_fields[1] .= scaling_function(
Regridder.read_from_hdf5(bcfile_dir, hd_outfile_root, all_dates[Int(midmonth_idx)], varname, comms_ctx),
bcf_info,
Expand All @@ -236,7 +235,9 @@ function update_midmonth_data!(date, bcf_info::BCFileInfo{FT}) where {FT}
Regridder.read_from_hdf5(bcfile_dir, hd_outfile_root, all_dates[Int(midmonth_idx + 1)], varname, comms_ctx),
bcf_info,
)
midmonth_idx = bcf_info.segment_idx[1] += Int(1)

# case 5: undefined condition
else
throw(ErrorException("Check boundary file specification"))
end
Expand Down
21 changes: 15 additions & 6 deletions test/bcreader_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ for FT in (Float32, Float64)
@test bcf_info.monthly_fields[2] == bcf_info.monthly_fields[1]
@test bcf_info.segment_length[1] == Int(0)

# case 2: date > all_dates[end - 1]
# case 2: date > all_dates[end]
reset_bcf_info(bcf_info)
date = DateTime(bcf_info.all_dates[end - 1] + Dates.Day(1))
date = DateTime(bcf_info.all_dates[end] + Dates.Day(1))
BCReader.update_midmonth_data!(date, bcf_info)

@test bcf_info.monthly_fields[1] == bcf_info.scaling_function(
Expand All @@ -267,20 +267,29 @@ for FT in (Float32, Float64)
@test bcf_info.monthly_fields[2] == bcf_info.monthly_fields[1]
@test bcf_info.segment_length[1] == Int(0)

# case 3: date - all_dates[segment_idx + 1] > 2
# case 3: date - all_dates[segment_idx + 1] > 2 days
reset_bcf_info(bcf_info)
date = DateTime(bcf_info.all_dates[bcf_info.segment_idx[1] + 1] + Dates.Day(3))
bcf_info.segment_idx[1] = bcf_info.segment_idx0[1]
date = DateTime(bcf_info.all_dates[bcf_info.segment_idx0[1] + 1] + Dates.Day(3))
BCReader.update_midmonth_data!(date, bcf_info)

nearest_idx = argmin(
abs.(
parse(FT, TimeManager.datetime_to_strdate(date)) .-
parse.(FT, TimeManager.datetime_to_strdate.(bcf_info.all_dates[:]))
),
)

@test bcf_info.segment_idx[1] == bcf_info.segment_idx0[1] == nearest_idx

# case 4: everything else
# case 4: Dates.days(date - all_dates[Int(midmonth_idx)]) > 0
reset_bcf_info(bcf_info)
bcf_info.segment_idx[1] = bcf_info.segment_idx0[1]
date = DateTime(bcf_info.all_dates[bcf_info.segment_idx0[1]] + Dates.Day(3))
BCReader.update_midmonth_data!(date, bcf_info)

@test bcf_info.segment_idx[1] == bcf_info.segment_idx0[1] + 1

# case 5: everything else
reset_bcf_info(bcf_info)
bcf_info.segment_idx[1] = bcf_info.segment_idx0[1] + Int(1)
date = bcf_info.all_dates[bcf_info.segment_idx[1]] - Dates.Day(1)
Expand Down

0 comments on commit 5eb309d

Please sign in to comment.