Skip to content

Commit e323940

Browse files
authored
Merge pull request #772 from CliMA/js/common-data
fix CommonDataModel bug
2 parents 75872f8 + 6629838 commit e323940

File tree

8 files changed

+71
-55
lines changed

8 files changed

+71
-55
lines changed

artifacts/artifact_funcs.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,24 @@ function pr_obs_data_path()
6161
end
6262

6363
"""
64-
artifact_data(datapath_full, name)
64+
artifact_data(datapath_full, filename)
6565
6666
Returns input dataset at datapath_full
6767
"""
68-
function artifact_data(datapath_full, name)
69-
datafile_truncated = joinpath(datapath_full, string(name, ".nc"))
68+
function artifact_data(datapath_full, filename)
69+
datafile_truncated = joinpath(datapath_full, string(lowercase(filename), ".nc"))
7070
return datafile_truncated
7171
end
7272

7373
"""
74-
artifact_data(datapath_full, name, datapath_trunc, date0, time_start, time_end, comms_ctx)
74+
artifact_data(datapath_full, filename, varname, datapath_trunc, date0, t_start, t_end, comms_ctx)
7575
76-
Truncates given data set, and constructs a new dataset containing only the dates needed and stores it in datapath_trunc
76+
Truncates given data set, and constructs a new dataset containing only
77+
the dates needed and stores it in datapath_trunc
7778
"""
78-
function artifact_data(datapath_full, name, datapath_trunc, date0, time_start, time_end, comms_ctx)
79-
datafile = joinpath(datapath_full, string(name, ".nc"))
79+
function artifact_data(datapath_full, filename, varname, datapath_trunc, date0, t_start, t_end, comms_ctx)
80+
datafile = joinpath(datapath_full, string(lowercase(filename), ".nc"))
8081
datafile_truncated =
81-
Regridder.truncate_dataset(datafile, name, datapath_trunc, date0, time_start, time_end, comms_ctx)
82+
Regridder.truncate_dataset(datafile, filename, varname, datapath_trunc, date0, t_start, t_end, comms_ctx)
8283
return datafile_truncated
8384
end

experiments/AMIP/Manifest.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,9 @@ version = "0.12.11"
478478

479479
[[deps.CommonDataModel]]
480480
deps = ["CFTime", "DataStructures", "Dates", "Preferences", "Printf", "Statistics"]
481-
git-tree-sha1 = "d7d7b58e149f19c322840a50d1bc20e8c23addb4"
481+
git-tree-sha1 = "d6fb5bf939a2753c74984b11434ea25d6c397a58"
482482
uuid = "1fbeeb36-5f17-413c-809b-666fb144f157"
483-
version = "0.3.5"
483+
version = "0.3.6"
484484

485485
[[deps.CommonSolve]]
486486
git-tree-sha1 = "0eee5eb66b1cf62cd6ad1b460238e60e4b09400c"

experiments/AMIP/coupler_driver.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ original sources.
163163
=#
164164

165165
include(joinpath(pkgdir(ClimaCoupler), "artifacts", "artifact_funcs.jl"))
166-
sst_data = artifact_data(sst_dataset_path(), "sst", dir_paths.regrid, date0, t_start, t_end, comms_ctx)
167-
sic_data = artifact_data(sic_dataset_path(), "sic", dir_paths.regrid, date0, t_start, t_end, comms_ctx)
168-
co2_data = artifact_data(co2_dataset_path(), "mauna_loa_co2", dir_paths.regrid, date0, t_start, t_end, comms_ctx)
166+
sst_data = artifact_data(sst_dataset_path(), "sst", "SST", dir_paths.regrid, date0, t_start, t_end, comms_ctx)
167+
sic_data = artifact_data(sic_dataset_path(), "sic", "SEAICE", dir_paths.regrid, date0, t_start, t_end, comms_ctx)
168+
co2_data = artifact_data(co2_dataset_path(), "mauna_loa_co2", "co2", dir_paths.regrid, date0, t_start, t_end, comms_ctx)
169169
land_mask_data = artifact_data(mask_dataset_path(), "seamask")
170170

171171
#=

experiments/ClimaCore/Manifest.toml

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
julia_version = "1.10.0"
44
manifest_format = "2.0"
5-
project_hash = "01a9ebd3f7699789bd6ed48bc9c7518a6ff5227e"
5+
project_hash = "89b3a4b3cb7163830203310d5b8b73e1b6be2a9d"
66

77
[[deps.ADTypes]]
88
git-tree-sha1 = "016833eb52ba2d6bea9fcb50ca295980e728ee24"
@@ -154,12 +154,6 @@ git-tree-sha1 = "389ad5c84de1ae7cf0e28e381131c98ea87d54fc"
154154
uuid = "fa961155-64e5-5f13-b03f-caf6b980ea82"
155155
version = "0.5.0"
156156

157-
[[deps.CFTime]]
158-
deps = ["Dates", "Printf"]
159-
git-tree-sha1 = "5afb5c5ba2688ca43a9ad2e5a91cbb93921ccfa1"
160-
uuid = "179af706-886a-5703-950a-314cd64e0468"
161-
version = "0.1.3"
162-
163157
[[deps.CPUSummary]]
164158
deps = ["CpuId", "IfElse", "PrecompileTools", "Static"]
165159
git-tree-sha1 = "601f7e7b3d36f18790e2caf83a882d88e9b71ff1"
@@ -279,12 +273,6 @@ git-tree-sha1 = "fc08e5930ee9a4e03f84bfb5211cb54e7769758a"
279273
uuid = "5ae59095-9a9b-59fe-a467-6f913c188581"
280274
version = "0.12.10"
281275

282-
[[deps.CommonDataModel]]
283-
deps = ["CFTime", "DataStructures", "Dates", "Preferences", "Printf", "Statistics"]
284-
git-tree-sha1 = "d7d7b58e149f19c322840a50d1bc20e8c23addb4"
285-
uuid = "1fbeeb36-5f17-413c-809b-666fb144f157"
286-
version = "0.3.5"
287-
288276
[[deps.CommonSolve]]
289277
git-tree-sha1 = "0eee5eb66b1cf62cd6ad1b460238e60e4b09400c"
290278
uuid = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2"

experiments/ClimaCore/Project.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
ClimaCore = "d414da3d-4745-48bb-8d80-42e94e092884"
33
ClimaParams = "5c42b081-d73a-476f-9059-fd94b934656c"
44
ClimaTimeSteppers = "595c0a79-7f3d-439a-bc5a-b232dc3bde79"
5-
CommonDataModel = "1fbeeb36-5f17-413c-809b-666fb144f157"
65
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
76
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
87
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
@@ -19,7 +18,6 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1918
[compat]
2019
ClimaParams = "0.10"
2120
ClimaTimeSteppers = "0.7"
22-
CommonDataModel = "=0.3.5"
2321
FileIO = "1.16"
2422
IntervalSets = "0.6, 0.7"
2523
LinearAlgebra = "1.0"

src/Regridder.jl

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -638,33 +638,62 @@ function cgll2latlonz(field; DIR = "cgll2latlonz_dir", nlat = 360, nlon = 720, c
638638
end
639639

640640
"""
641-
truncate_dataset(datafile, name, datapath_trunc, date0, time_start, time_end, comms_ctx)
641+
truncate_dataset(datafile, filename, varname, datapath_trunc, date0, t_start, t_end, comms_ctx)
642642
643-
Truncates given data set, and constructs a new dataset containing only the dates that are used in the simulation
643+
Truncates given data set, and constructs a new dataset containing only
644+
the dates that are used in the simulation
644645
"""
645646
function truncate_dataset(
646647
datafile,
647-
name,
648+
filename,
649+
varname,
648650
datapath_trunc,
649651
date0,
650-
time_start,
651-
time_end,
652+
t_start,
653+
t_end,
652654
comms_ctx::ClimaComms.AbstractCommsContext,
653655
)
654-
date_start = date0 + Dates.Second(time_start)
655-
date_end = date0 + Dates.Second(time_start + time_end)
656+
date_start = date0 + Dates.Second(t_start)
657+
date_end = date0 + Dates.Second(t_start + t_end)
656658

657-
file_name = replace(string(name, "_truncated_data_", string(date_start), string(date_end), ".nc"), r":" => "")
658-
datafile_truncated = joinpath(datapath_trunc, file_name)
659+
filename_truncated = replace(
660+
string(lowercase(filename), "_truncated_data_", string(date_start), string(date_end), ".nc"),
661+
r":" => "",
662+
)
663+
datafile_truncated = joinpath(datapath_trunc, filename_truncated)
659664

660665
if ClimaComms.iamroot(comms_ctx)
661666
ds = NCDatasets.NCDataset(datafile, "r")
662667
dates = ds["time"][:]
663668

669+
# Find the bounding indices of the dates we need
664670
(start_id, end_id) = find_idx_bounding_dates(dates, date_start, date_end)
665671

672+
var_truncated = NCDatasets.nomissing(NCDatasets.view(ds, time = start_id:end_id)[varname])
673+
674+
# Create new dataset to fill with truncated data
666675
ds_truncated = NCDatasets.NCDataset(datafile_truncated, "c")
667-
ds_truncated = NCDatasets.write(ds_truncated, NCDatasets.view(ds, time = start_id:end_id))
676+
677+
# Keep all dimensions of original dataset (except for time, which we truncate)
678+
ds_dim_names = NCDatasets.dimnames(ds[varname])
679+
for dim_name in ds_dim_names
680+
dim_name != "time" && NCDatasets.defDim(ds_truncated, dim_name, ds.dim[dim_name])
681+
end
682+
dates_truncated = dates[start_id:end_id]
683+
NCDatasets.defDim(ds_truncated, "time", length(dates_truncated))
684+
ds_truncated.attrib["title"] = ds.attrib["title"] * " (dates truncated)"
685+
686+
# Define dimension variables
687+
for dim_name in ds_dim_names
688+
if dim_name == "time"
689+
var = NCDatasets.defVar(ds_truncated, dim_name, dates_truncated, (dim_name,))
690+
else
691+
var = NCDatasets.defVar(ds_truncated, dim_name, ds[dim_name][:], (dim_name,))
692+
end
693+
end
694+
695+
# Create variable of interest in new dataset, and fill with input dataset values
696+
var = NCDatasets.defVar(ds_truncated, varname, var_truncated, ds_dim_names)
668697

669698
close(ds)
670699
close(ds_truncated)

test/Project.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ ClimaCoupler = "4ade58fe-a8da-486c-bd89-46df092ec0c7"
1313
ClimaLand = "08f4d4ce-cf43-44bb-ad95-9d2d5f413532"
1414
ClimaParams = "5c42b081-d73a-476f-9059-fd94b934656c"
1515
ClimaTimeSteppers = "595c0a79-7f3d-439a-bc5a-b232dc3bde79"
16-
CommonDataModel = "1fbeeb36-5f17-413c-809b-666fb144f157"
1716
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
1817
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
1918
GeoMakie = "db073c08-6b98-4ee5-b6a4-5efafb3259c6"
@@ -43,7 +42,6 @@ ArtifactWrappers = "0.2"
4342
ClimaAtmos = "0.23"
4443
ClimaLand = "0.11"
4544
ClimaParams = "0.10"
46-
CommonDataModel = "=0.3.5"
4745
Dates = "1"
4846
Downloads = "1"
4947
IntervalSets = "0.5, 0.6, 0.7"

test/regridder_tests.jl

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -312,40 +312,42 @@ for FT in (Float32, Float64)
312312
end
313313
end
314314
end
315-
# test dataset truncation
315+
316+
# test dataset truncation
316317
@testset "test dataset truncation" begin
317-
# Get the original dataset set up
318+
# Get the original dataset set up
318319
include(joinpath(pkgdir(ClimaCoupler), "artifacts", "artifact_funcs.jl"))
319320
sst_data_all = joinpath(sst_dataset_path(), "sst.nc")
320321
ds = NCDatasets.NCDataset(sst_data_all, "r")
321322
dates = ds["time"][:]
322323
first_date = dates[1]
323324
last_date = last(dates)
324325

325-
# set up comms_ctx
326+
# set up comms_ctx
326327
device = ClimaComms.device()
327-
comms_ctx = ClimaComms.context(device)
328-
ClimaComms.init(comms_ctx)
328+
comms_ctx_device = ClimaComms.context(device)
329+
ClimaComms.init(comms_ctx_device)
329330

330-
# make path for truncated datasets
331+
# make path for truncated datasets
331332
COUPLER_OUTPUT_DIR = joinpath("experiments", "AMIP", "output", "tests")
332333
mkpath(COUPLER_OUTPUT_DIR)
333334

334335
REGRID_DIR = joinpath(COUPLER_OUTPUT_DIR, "regrid_tmp", "")
335336
mkpath(REGRID_DIR)
336337

337-
# values for the truncations
338-
time_start = 0.0
339-
time_end = 1.728e6
338+
# values for the truncations
339+
t_start = 0.0
340+
t_end = 1.728e6
340341
date0test = ["18690101", "18700101", "19790228", "20220301", "20230101"]
341342
for date in date0test
342343
date0 = Dates.DateTime(date, Dates.dateformat"yyyymmdd")
343-
sst_data = Regridder.truncate_dataset(sst_data_all, "test", REGRID_DIR, date0, time_start, time_end, comms_ctx)
344+
sst_data =
345+
Regridder.truncate_dataset(sst_data_all, "sst", "SST", REGRID_DIR, date0, t_start, t_end, comms_ctx_device)
344346
ds_truncated = NCDatasets.NCDataset(sst_data, "r")
345347
new_dates = ds_truncated["time"][:]
346348

347-
date_start = date0 + Dates.Second(time_start)
348-
date_end = date0 + Dates.Second(time_start + time_end)
349+
date_start = date0 + Dates.Second(t_start)
350+
date_end = date0 + Dates.Second(t_start + t_end)
349351

350352
# start date is before the first date of datafile
351353
if date_start < first_date
@@ -359,10 +361,10 @@ end
359361
@test new_dates[2] >= date_start
360362
end
361363

362-
# end date is before the first date of datafile
364+
# end date is before the first date of datafile
363365
if date_end < first_date
364366
@test last(new_dates) == first_date
365-
# end date is after the last date of datafile
367+
# end date is after the last date of datafile
366368
elseif date_end > last_date
367369
@test last(new_dates) == last_date
368370
# end date is within the bounds of datafile
@@ -371,7 +373,7 @@ end
371373
@test new_dates[length(new_dates) - 1] <= date_end
372374
end
373375

374-
# check that truncation is indexing correctly
376+
# check that truncation is indexing correctly
375377
all_data = ds["SST"][:, :, :]
376378
new_data = ds_truncated["SST"][:, :, :]
377379
(start_id, end_id) = Regridder.find_idx_bounding_dates(dates, date_start, date_end)

0 commit comments

Comments
 (0)