@@ -927,8 +927,6 @@ compute_diagnostics(const bool allow_invalid_fields)
927
927
void AtmosphereOutput::
928
928
process_requested_fields (const std::string& stream_name)
929
929
{
930
- using stratts_t = std::map<std::string,std::string>;
931
-
932
930
// So far, all fields (on the output grid) that ARE in the model FM have been added
933
931
// to the FM stored in this class for the FromModel phase. Anything missing
934
932
// must be either a diagnostic or an alias.
@@ -1071,7 +1069,10 @@ process_requested_fields(const std::string& stream_name)
1071
1069
remaining.insert (it.second );
1072
1070
}
1073
1071
while (not done) {
1074
- std::set<std::string> remove_these;
1072
+ // We can't add-to/rm-form a std:;set while iterating on it, as that could
1073
+ // change the end iterator. Hence, keep track of what we add or remove,
1074
+ // and add/remove after the for loop ends
1075
+ std::set<std::string> remove_these, add_these;
1075
1076
for (const auto & name : remaining) {
1076
1077
if (fm_model->has_field (name)) {
1077
1078
// This is a regular field, not a diagnostic nor an alias.
@@ -1096,8 +1097,15 @@ process_requested_fields(const std::string& stream_name)
1096
1097
auto diag = name2diag[name];
1097
1098
bool ready_to_init = true ;
1098
1099
for (const auto & freq : diag->get_required_field_requests ()) {
1099
- if (not fm_model->has_field (freq.fid .name ())) {
1100
+ const auto & dep_name = freq.fid .name ();
1101
+ if (not fm_model->has_field (dep_name)) {
1100
1102
ready_to_init = false ;
1103
+ if (not remaining.count (dep_name)) {
1104
+ // This requirement MUST be another diagnostic, since it was not in the original m_fields_names
1105
+ // Create the diag, and add it to the list of fields to process
1106
+ name2diag[freq.fid .name ()] = create_diagnostic (freq.fid .name (),fm_model->get_grid ());
1107
+ add_these.insert (freq.fid .name ());
1108
+ }
1101
1109
break ;
1102
1110
}
1103
1111
}
@@ -1108,13 +1116,16 @@ process_requested_fields(const std::string& stream_name)
1108
1116
}
1109
1117
}
1110
1118
1111
- EKAT_REQUIRE_MSG (remove_these.size ()>0 ,
1119
+ EKAT_REQUIRE_MSG (add_these. size ()> 0 or remove_these.size ()>0 ,
1112
1120
" Error! We're stuck in an endless loop while processing output fields.\n "
1113
1121
" - stream name: " + stream_name + " \n " );
1114
1122
1115
1123
for (const auto & n : remove_these) {
1116
1124
remaining.erase (n);
1117
1125
}
1126
+ for (const auto & n : add_these) {
1127
+ remaining.insert (n);
1128
+ }
1118
1129
1119
1130
done = remaining.size ()==0 ;
1120
1131
}
0 commit comments