Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where the program would fail if a CommIn file defines only flexible inputs without any fixed inputs. The fix generalizes the existing approach of creating empty arrays for missing input types.
Changes:
- Modified
process_io_technodatato handle scenarios where only flexible inputs are defined - Added symmetric logic to create zero arrays for whichever input type (fixed or flexible) is missing
- Added error handling for the edge case where neither fixed nor flexible levels are found
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # Ensure both `fixed` and `flexible` inputs/outputs are defined. If only one is | ||
| # defined in the input data, create the other as a zeros array with the same shape. | ||
| has_fixed = "fixed" in result.data_vars | ||
| has_flexible = "flexible" in result.data_vars | ||
| if has_fixed and not has_flexible: | ||
| result["flexible"] = xr.zeros_like(result.fixed).rename("flexible") | ||
| elif has_flexible and not has_fixed: | ||
| result["fixed"] = xr.zeros_like(result.flexible).rename("fixed") | ||
| elif not has_fixed and not has_flexible: | ||
| raise ValueError("Neither 'fixed' nor 'flexible' levels were found.") |
There was a problem hiding this comment.
The new behavior for handling models with only flexible inputs (lines 578-579) lacks test coverage. While the existing test test_read_io_technodata verifies the standard case with both fixed and flexible inputs, there should be a test case that verifies the scenario where only flexible inputs are defined, ensuring that fixed inputs are correctly created as zeros.
Currently, the program will fail if a
CommInfile only defines flexible inputs, without any fixed inputs, with this error:ValueError: cannot rename 'fixed' because it is not a variable or dimension in this datasetThis is a valid scenario so we should support this. We currently allow having just fixed inputs without any flexible inputs, by creating an empty (all-zeros) array of flexible inputs in this scenario. This PR generalises this approach by doing an analagous step in the case that only flexible inputs are defined.