Skip to content

Commit 00779f0

Browse files
committed
EAMxx: fix accumul for non-diag fields
1 parent bb1e9de commit 00779f0

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

components/eamxx/src/physics/cosp/eamxx_cosp.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ void Cosp::initialize_impl (const RunType /* run_type */)
113113
for (const auto& field_name : vnames) {
114114
// the mask here is just the sunlit mask, so set it
115115
get_field_out(field_name).get_header().set_extra_data("mask_field", get_field_in("sunlit_mask"));
116+
get_field_out(field_name).get_header().set_may_be_filled(true);
116117
}
117118
}
118119

components/eamxx/src/share/io/scorpio_output.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ AtmosphereOutput (const ekat::Comm& comm, const ekat::ParameterList& params,
146146
EKAT_REQUIRE_MSG (not has_duplicates(m_fields_names),
147147
"[AtmosphereOutput] Error! One of the output yaml files has duplicate field entries.\n"
148148
" - yaml file: " + params.name() + "\n"
149-
" - fields names; " + ekat::join(m_fields_names,",") + "\n");
149+
" - fields names: " + ekat::join(m_fields_names,",") + "\n");
150150

151151
// Check if remapping and if so create the appropriate remapper
152152
// Note: We currently support three remappers
@@ -174,6 +174,7 @@ AtmosphereOutput (const ekat::Comm& comm, const ekat::ParameterList& params,
174174
}
175175

176176
// ... then add diagnostic fields
177+
// ... and we also use this to init track avg cnt for regular fields
177178
init_diagnostics ();
178179

179180
// Avg count only makes sense if we have
@@ -508,6 +509,16 @@ run (const std::string& filename,
508509
const auto& f_in = fm_after_hr->get_field(field_name);
509510
auto& f_out = fm_scorpio->get_field(field_name);
510511

512+
// Safety check: if a field may contain fill values and we are computing an Average,
513+
// we must have created an avg-count tracking field; otherwise division by the raw
514+
// number of steps would bias the result wherever fill values occurred.
515+
if (m_avg_type==OutputAvgType::Average && f_in.get_header().may_be_filled()) {
516+
EKAT_REQUIRE_MSG(m_field_to_avg_count.count(field_name),
517+
"[AtmosphereOutput::run] Error! Averaging a fill-aware field without avg-count tracking.\n"
518+
" - field name : " + field_name + "\n"
519+
"This indicates the field was marked may_be_filled after output initialization or tracking logic missed it." );
520+
}
521+
511522
switch (m_avg_type) {
512523
case OutputAvgType::Instant:
513524
f_out.deep_copy(f_in); break; // Note: if f_in aliases f_out, this is a no-op
@@ -1003,6 +1014,26 @@ init_diagnostics ()
10031014
for (const auto& fname : m_fields_names) {
10041015
if (not m_field_mgrs[FromModel]->has_field(fname)) {
10051016
create_diag(fname);
1017+
} else {
1018+
// This is a regular field, not a diagnostic.
1019+
// Still, we might need to do some extra setup, like for avg_count.
1020+
const auto& f = m_field_mgrs[FromModel]->get_field(fname);
1021+
// We need avg-count tracking for any averaged (non-instant) field that:
1022+
// - supplies explicit mask info (mask_data or mask_field), OR
1023+
// - is marked as potentially containing fill values (may_be_filled()).
1024+
// Without this, fill-aware updates skip fill_value during accumulation (good)
1025+
// but we would still divide by the raw nsteps, biasing the result low.
1026+
if (m_avg_type!=OutputAvgType::Instant) {
1027+
const bool has_mask = f.get_header().has_extra_data("mask_data") || f.get_header().has_extra_data("mask_field");
1028+
const bool may_be_filled = f.get_header().may_be_filled();
1029+
if (has_mask || may_be_filled) {
1030+
m_track_avg_cnt = true;
1031+
// Avoid duplicate insertion if already present (e.g., mask + filled both true)
1032+
if (m_field_to_avg_cnt_suffix.count(f.name())==0) {
1033+
m_field_to_avg_cnt_suffix.emplace(f.name(), "_" + f.name());
1034+
}
1035+
}
1036+
}
10061037
}
10071038
}
10081039
}

0 commit comments

Comments
 (0)