Skip to content

Commit 73dd7ab

Browse files
authored
Merge pull request #200 from CliMA/kp/short-names
Add ObservationRecipe.short_names
2 parents f81efa7 + 1d79acc commit 73dd7ab

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

ext/ClimaAnalysisExt.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,28 @@ function ObservationRecipe.observation(
304304
)
305305
end
306306

307+
"""
308+
short_names(obs::EKP.Observation)
309+
310+
Get the short names of the variables from the metadata in the `EKP.Observation`.
311+
312+
If the short name is not available, then `nothing` is returned instead.
313+
"""
314+
function ObservationRecipe.short_names(obs::EKP.Observation)
315+
# Get the short names from the metadata rather than the name of the
316+
# observation, because the name can be changed by the user
317+
metadatas = EKP.get_metadata(obs)
318+
eltype(metadatas) <: ClimaAnalysis.Var.Metadata || error(
319+
"Getting the short names from an observation is only supported with metadata from ClimaAnalysis",
320+
)
321+
322+
short_names = collect(
323+
get(metadata.attributes, "short_name", nothing) for
324+
metadata in metadatas
325+
)
326+
return short_names
327+
end
328+
307329
"""
308330
seasonally_aligned_yearly_sample_date_ranges(var::OutputVar)
309331

src/observation_recipe.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export ScalarCovariance,
55
SVDplusDCovariance,
66
covariance,
77
observation,
8+
short_names,
89
seasonally_aligned_yearly_sample_date_ranges,
910
change_data_type
1011

@@ -275,6 +276,8 @@ function covariance end
275276

276277
function observation end
277278

279+
function short_names end
280+
278281
function seasonally_aligned_yearly_sample_date_ranges end
279282

280283
function change_data_type end

test/observation_recipe.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,3 +1066,42 @@ end
10661066
)
10671067

10681068
end
1069+
1070+
@testset "Short names of observation" begin
1071+
if pkgversion(EnsembleKalmanProcesses) > v"2.4.2"
1072+
time =
1073+
ClimaAnalysis.Utils.date_to_time.(
1074+
Dates.DateTime(2007, 12),
1075+
[Dates.DateTime(2007, 12) + Dates.Month(i) for i in 0:2],
1076+
)
1077+
var1 =
1078+
TemplateVar() |>
1079+
add_dim("time", time, units = "s") |>
1080+
add_attribs(short_name = "Hello", start_date = "2007-12-1") |>
1081+
initialize
1082+
1083+
var2 =
1084+
TemplateVar() |>
1085+
add_dim("time", time, units = "s") |>
1086+
add_attribs(short_name = "world!", start_date = "2007-12-1") |>
1087+
initialize
1088+
1089+
var3 =
1090+
TemplateVar() |>
1091+
add_dim("time", time, units = "s") |>
1092+
add_attribs(no_short_name = "no", start_date = "2007-12-1") |>
1093+
initialize
1094+
1095+
covar_estimator = ObservationRecipe.ScalarCovariance()
1096+
obs = ObservationRecipe.observation(
1097+
covar_estimator,
1098+
(var1, var2, var3),
1099+
Dates.DateTime(2007, 12, 1),
1100+
Dates.DateTime(2008, 1, 1),
1101+
)
1102+
@test isequal(
1103+
ObservationRecipe.short_names(obs),
1104+
["Hello", "world!", nothing],
1105+
)
1106+
end
1107+
end

0 commit comments

Comments
 (0)