Skip to content

Commit

Permalink
edits
Browse files Browse the repository at this point in the history
  • Loading branch information
anastasia-popova committed Apr 3, 2024
1 parent 5c20fc8 commit 34ec62e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/Regridder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/regridder_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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

0 comments on commit 34ec62e

Please sign in to comment.