Skip to content

Commit

Permalink
use t_start/end
Browse files Browse the repository at this point in the history
  • Loading branch information
juliasloan25 committed May 14, 2024
1 parent c5dfb63 commit 937cd48
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
7 changes: 3 additions & 4 deletions artifacts/artifact_funcs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,12 @@ function artifact_data(datapath_full, name)
end

"""
artifact_data(datapath_full, name, datapath_trunc, date0, time_start, time_end, comms_ctx)
artifact_data(datapath_full, name, datapath_trunc, date0, t_start, t_end, comms_ctx)
Truncates given data set, and constructs a new dataset containing only the dates needed and stores it in datapath_trunc
"""
function artifact_data(datapath_full, name, datapath_trunc, date0, time_start, time_end, comms_ctx)
function artifact_data(datapath_full, name, datapath_trunc, date0, t_start, t_end, comms_ctx)
datafile = joinpath(datapath_full, string(name, ".nc"))
datafile_truncated =
Regridder.truncate_dataset(datafile, name, datapath_trunc, date0, time_start, time_end, comms_ctx)
datafile_truncated = Regridder.truncate_dataset(datafile, name, datapath_trunc, date0, t_start, t_end, comms_ctx)
return datafile_truncated
end
10 changes: 5 additions & 5 deletions src/Regridder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ function cgll2latlonz(field; DIR = "cgll2latlonz_dir", nlat = 360, nlon = 720, c
end

"""
truncate_dataset(datafile, name, datapath_trunc, date0, time_start, time_end, comms_ctx)
truncate_dataset(datafile, name, datapath_trunc, date0, t_start, t_end, comms_ctx)
Truncates given data set, and constructs a new dataset containing only the dates that are used in the simulation
"""
Expand All @@ -647,12 +647,12 @@ function truncate_dataset(
name,
datapath_trunc,
date0,
time_start,
time_end,
t_start,
t_end,
comms_ctx::ClimaComms.AbstractCommsContext,
)
date_start = date0 + Dates.Second(time_start)
date_end = date0 + Dates.Second(time_start + time_end)
date_start = date0 + Dates.Second(t_start)
date_end = date0 + Dates.Second(t_start + t_end)

file_name = replace(string(name, "_truncated_data_", string(date_start), string(date_end), ".nc"), r":" => "")
datafile_truncated = joinpath(datapath_trunc, file_name)
Expand Down
26 changes: 13 additions & 13 deletions test/regridder_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -312,40 +312,40 @@ for FT in (Float32, Float64)
end
end
end
# test dataset truncation
# test dataset truncation
@testset "test dataset truncation" begin
# Get the original dataset set up
# Get the original dataset set up
include(joinpath(pkgdir(ClimaCoupler), "artifacts", "artifact_funcs.jl"))
sst_data_all = joinpath(sst_dataset_path(), "sst.nc")
ds = NCDatasets.NCDataset(sst_data_all, "r")
dates = ds["time"][:]
first_date = dates[1]
last_date = last(dates)

# set up comms_ctx
# set up comms_ctx
device = ClimaComms.device()
comms_ctx = ClimaComms.context(device)
ClimaComms.init(comms_ctx)

# make path for truncated datasets
# make path for truncated datasets
COUPLER_OUTPUT_DIR = joinpath("experiments", "AMIP", "output", "tests")
mkpath(COUPLER_OUTPUT_DIR)

REGRID_DIR = joinpath(COUPLER_OUTPUT_DIR, "regrid_tmp", "")
mkpath(REGRID_DIR)

# values for the truncations
time_start = 0.0
time_end = 1.728e6
# values for the truncations
t_start = 0.0
t_end = 1.728e6
date0test = ["18690101", "18700101", "19790228", "20220301", "20230101"]
for date in date0test
date0 = Dates.DateTime(date, Dates.dateformat"yyyymmdd")
sst_data = Regridder.truncate_dataset(sst_data_all, "test", REGRID_DIR, date0, time_start, time_end, comms_ctx)
sst_data = Regridder.truncate_dataset(sst_data_all, "test", REGRID_DIR, date0, t_start, t_end, comms_ctx)
ds_truncated = NCDatasets.NCDataset(sst_data, "r")
new_dates = ds_truncated["time"][:]

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

# start date is before the first date of datafile
if date_start < first_date
Expand All @@ -359,10 +359,10 @@ end
@test new_dates[2] >= date_start
end

# end date is before the first date of datafile
# end date is before the first date of datafile
if date_end < first_date
@test last(new_dates) == first_date
# end date is after the last date of datafile
# end date is after the last date of datafile
elseif date_end > last_date
@test last(new_dates) == last_date
# end date is within the bounds of datafile
Expand All @@ -371,7 +371,7 @@ end
@test new_dates[length(new_dates) - 1] <= date_end
end

# check that truncation is indexing correctly
# check that truncation is indexing correctly
all_data = ds["SST"][:, :, :]
new_data = ds_truncated["SST"][:, :, :]
(start_id, end_id) = Regridder.find_idx_bounding_dates(dates, date_start, date_end)
Expand Down

0 comments on commit 937cd48

Please sign in to comment.