From 34ec62e62057e969c4b91fa97d93750fede9fb08 Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Wed, 3 Apr 2024 15:23:42 -0400 Subject: [PATCH] edits --- src/Regridder.jl | 30 ++++++++++++++++++++++-------- test/regridder_tests.jl | 9 +++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/Regridder.jl b/src/Regridder.jl index 18ee558b1..1c6dcbc10 100644 --- a/src/Regridder.jl +++ b/src/Regridder.jl @@ -645,24 +645,38 @@ function truncate_dataset(date0, time_start, time_end, datafile, datapath) ds = NCDataset(datafile, "r") dates = ds["time"][:] - if date_start < dates[1] # if the simulation start date is before our first date in the dataset, we leave the beginning of the truncated dataset to be the first date available + # if the simulation start date is before our first date in the dataset + # leave the beginning of the truncated dataset to be first date available + if date_start < dates[1] start_id = 1 - elseif date_start > last(dates) # if the simulation start date is after the last date in the dataset, we start the truncated dataset at its last possible date + # if the simulation start date is after the last date in the dataset + # start the truncated dataset at its last possible date + elseif date_start > last(dates) start_id = length(dates) - else # if the simulation start date falls within the range of the dataset, we find the closest date to the start date and truncate there + # if the simulation start date falls within the range of the dataset + # find the closest date to the start date and truncate there + else (~, start_id) = findmin(x -> abs(x - date_start), dates) - if dates[start_id] > date_start # if the closest date is after the start date, we add one more date before it to the dataset + # if the closest date is after the start date, add one more date before + if dates[start_id] > date_start start_id = start_id - 1 end end - if date_end < dates[1] # if the simulation end date is before our first date in the dataset, we truncate the end of the dataset to be the first date + # if the simulation end date is before our first date in the dataset + # truncate the end of the dataset to be the first date + if date_end < dates[1] end_id = 1 - elseif date_end > last(dates) # if the simulation end date is after the last date in the dataset, we leave the end of the dataset as is + # if the simulation end date is after the last date in the dataset + # leave the end of the dataset as is + elseif date_end > last(dates) end_id = length(dates) - else # if the simulation end date falls within the range of the dataset, we find the closest date to the end date and truncate there + # if the simulation end date falls within the range of the dataset + # find the closest date to the end date and truncate there + else (~, end_id) = findmin(x -> abs(x - date_end), dates) - if dates[end_id] < date_end # if the closest date is before the end date, we add one more date after it to the dataset + # if the closest date is before the end date, add one more date after + if dates[end_id] < date_end end_id = end_id + 1 end end diff --git a/test/regridder_tests.jl b/test/regridder_tests.jl index a6227f957..9e9548ef3 100644 --- a/test/regridder_tests.jl +++ b/test/regridder_tests.jl @@ -328,6 +328,12 @@ end # values for the truncations time_start = 0.0 time_end = 1.728e6 + # tests five cases: + # 1. both start and end are earlier than in datafile + # 2. start is early, end is within datafine + # 3. both start and end within datafile + # 4. start is in datafile end is not + # 5. both start and end are later than in datafile date0test = ["18690101", "18700101", "19790228", "20220301", "20230101"] for date in date0test date0 = DateTime(date, dateformat"yyyymmdd") @@ -355,5 +361,8 @@ end @test last(new_dates) >= date_end @test new_dates[length(new_dates) - 1] <= date_end end + close(nds) end + + close(ds) end