Skip to content

Commit

Permalink
test passing
Browse files Browse the repository at this point in the history
  • Loading branch information
anastasia-popova committed Apr 2, 2024
1 parent d1fb094 commit 5c20fc8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 46 deletions.
11 changes: 5 additions & 6 deletions src/Regridder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ function truncate_dataset(date0, time_start, time_end, datafile, datapath)
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
(~, 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
start_id = start_id - 1
start_id = start_id - 1
end
end

Expand All @@ -663,17 +663,16 @@ function truncate_dataset(date0, time_start, time_end, datafile, datapath)
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
(~, 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
end_id = end_id + 1
end_id = end_id + 1
end
end

println("start_id = ", start_id, " to end_id = ", end_id)

datafile_truncated = joinpath(datapath, "truncated_data.nc")
file_name = string("truncated_data_", string(date_start), string(date_end), ".nc")
datafile_truncated = joinpath(datapath, file_name)
truncated_ds = NCDataset(datafile_truncated, "c")
truncated_ds = write(truncated_ds, view(ds, time = start_id:end_id))


truncated_path = path(truncated_ds)

close(ds)
Expand Down
79 changes: 39 additions & 40 deletions test/regridder_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,47 +314,46 @@ for FT in (Float32, Float64)

end
end
end
# test dataset truncation
@testset "test dataset truncation" begin
# Get the original dataset set up
include(joinpath(pkgdir(ClimaCoupler), "artifacts", "artifact_funcs.jl"))
sst_data_all = joinpath(sst_dataset_path(), "sst.nc")
ds = NCDataset(sst_data_all, "r")
dates = ds["time"][:]
first_date = dates[1]
last_date = last(dates)

# values for the truncations
time_start = 0.0
time_end = 1.728e6
date0test = ["18690101", "18700101", "19790228", "20220301", "20230101"]
for date in date0test
date0 = DateTime(date, dateformat"yyyymmdd")
sst_data = Regridder.truncate_dataset(date0, time_start, time_end, sst_data_all, sst_dataset_path())
nds = NCDataset(sst_data, "r")
new_dates = nds["time"][:]

date_start = date0 + Dates.Second(time_start)
date_end = date0 + Dates.Second(time_start + time_end)

if date_start < first_date
@test new_dates[1] == first_date
elseif date_start > last_date
@test new_dates[1] == last_date
else
@test new_dates[1] <= date_start
@test new_dates[2] >= date_start
end

# test dataset truncation
@testset "test dataset truncation" begin
# Get the original dataset set up
include(joinpath(pkgdir(ClimaCoupler), "artifacts", "artifact_funcs.jl"))
sst_data_all = joinpath(sst_dataset_path(), "sst.nc")
ds = NCDataset(sst_data_all, "r")
dates = ds["time"][:]
first_date = dates[1]
last_date = last(dates)

# values for the truncations
time_start = 0.0
time_end = 1.728e6
date0test = ["18690101", "18700101", "19790301", "20220301", "20230101"]
for date in date0test
date0 = DateTime(date, dateformat"yyyymmdd")
sst_data = Regridder.truncate_dataset(date0, time_start, time_end, sst_data_all, sst_dataset_path())
nds = NCDataset(sst_data, "r")
new_dates = nds["time"][:]

date_start = date0 + Dates.Second(time_start)
date_end = date0 + Dates.Second(time_start + time_end)

if date_start < first_date
@test new_dates[1] == first_date
elseif date_start > last_date
@test new_dates[1] == last_date
else
@test new_dates[1] <= date_start
@test new_dates[2] >= date_start
end

if date_end < first_date
@test last(new_dates) == first_date
elseif date_end > last_date
@test last(new_dates) == last_date
else
@test last(new_dates) >= date_end
@test new_dates[length(new_dates) - 1] <= date_end
end
if date_end < first_date
@test last(new_dates) == first_date
elseif date_end > last_date
@test last(new_dates) == last_date
else
@test last(new_dates) >= date_end
@test new_dates[length(new_dates) - 1] <= date_end
end
end
end

0 comments on commit 5c20fc8

Please sign in to comment.