Skip to content

Commit 7fe2021

Browse files
committed
Add error handling for duplicate dates
Before, the function Var.shift_to_start_of_previous_month does not has error handling for duplicate dates.
1 parent 6c98e99 commit 7fe2021

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var_no_ocean = ClimaAnalysis.apply_oceanmask(var)
2222

2323
## Bug fixes
2424
- Masking now affects the colorbar.
25+
- `Var.shift_to_start_of_previous_month` now checks for duplicate dates and throws an error
26+
if duplicate dates are detected.
2527

2628
v0.5.10
2729
-------

src/Var.jl

+6
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,12 @@ function shift_to_start_of_previous_month(var::OutputVar)
14511451
date_arr .=
14521452
date_arr .|> Dates.firstdayofmonth .|> date -> date - Dates.Month(1)
14531453

1454+
# Check for duplicate dates
1455+
unique_dates = unique(date_arr)
1456+
unique_dates != date_arr && error(
1457+
"Dates are not unique after applying shift_to_start_of_previous_month",
1458+
)
1459+
14541460
# Convert from dates to seconds
14551461
start_date = date_arr[begin]
14561462
time_arr = map(date -> date_to_time(start_date, date), date_arr)

test/test_Var.jl

+17
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,23 @@ end
15051505
@test_throws ErrorException ClimaAnalysis.shift_to_start_of_previous_month(
15061506
var_min,
15071507
)
1508+
1509+
# Duplicate dates after applying transformations
1510+
time_arr = [
1511+
Dates.DateTime("2010-01-02T00:00:00"),
1512+
Dates.DateTime("2010-01-21T00:00:00"),
1513+
Dates.DateTime("2010-01-31T00:00:00"),
1514+
]
1515+
data = ones(length(time_arr))
1516+
dims = OrderedDict("time" => time_arr)
1517+
dim_attribs = OrderedDict("time" => Dict("blah" => "blah"))
1518+
attribs =
1519+
Dict("long_name" => "idk", "short_name" => "short", "units" => "kg")
1520+
var = ClimaAnalysis.OutputVar(attribs, dims, dim_attribs, data)
1521+
var_s = ClimaAnalysis.Var._dates_to_seconds(var)
1522+
@test_throws ErrorException ClimaAnalysis.shift_to_start_of_previous_month(
1523+
var_s,
1524+
)
15081525
end
15091526

15101527
@testset "Land and ocean masks" begin

0 commit comments

Comments
 (0)