Skip to content

fix: support conditional UKV renaming with tests #364#393

Merged
dfulu merged 6 commits intoopenclimatefix:mainfrom
ArkVex:fix/ukv-naming-364
Feb 4, 2026
Merged

fix: support conditional UKV renaming with tests #364#393
dfulu merged 6 commits intoopenclimatefix:mainfrom
ArkVex:fix/ukv-naming-364

Conversation

@ArkVex
Copy link
Contributor

@ArkVex ArkVex commented Feb 3, 2026

Pull Request

Description

Fixes #364

This PR implements conditional renaming for UKV data coordinates in the open_ukv loader. Previously, the code strictly expected legacy dimension names (x, y), which caused KeyError exceptions when attempting to load newer UKV datasets that already use the x_osgb and y_osgb naming convention.

Changes:

  • Replaced the hardcoded .rename() call with a dictionary comprehension that checks for the existence of dimensions/coordinates before attempting to rename them.
  • This ensures backward compatibility with legacy data while natively supporting new datasets.
  • Added a dedicated test suite to verify both naming scenarios.

How Has This Been Tested?

I have verified the fix by creating a new test file tests/load/nwp/providers/test_ukv.py which includes:

  • Legacy Data Test: Verified that datasets with x, y, init_time, and variable are correctly renamed.
  • New Data Test: Verified that datasets already using the new naming convention are handled without errors or redundant renames.

Tests were verified locally using pytest.

  • Yes

If your changes affect data processing, have you plotted any changes? i.e. have you done a quick sanity check?

  • Yes - Verified coordinate names and data structure consistency in xarray.

Checklist:

  • My code follows OCF's coding style guidelines
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked my code and corrected any misspellings

CC @dfulu plz review

import xarray as xr


def test_ukv_rename_logic_legacy():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ArkVex these tests are not best practice. Its standard to test the actual function rather than the logic internal to the function and these tests would be a pain to maintain. The legacy version of this function will already be run in this test. You could create another one which lives alongside that test which tests the newer coords


# Only rename if the source key exists in the dataset's dimensions or coordinates
# This prevents KeyErrors when the new UKV data already has "x_osgb" and "y_osgb"
actual_rename = {k: v for k, v in rename_map.items() if k in ds.dims or k in ds.coords}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some redundancy here. It would be simpler and good enough just to check if k in ds.coords

Copy link
Member

@dfulu dfulu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution @ArkVex, I've left a few comments I'd like to see addressesd before this is ready to go in

@ArkVex
Copy link
Contributor Author

ArkVex commented Feb 4, 2026

@dfulu plz check the applied changes

# This prevents KeyErrors when the new UKV data already has "x_osgb" and "y_osgb"
actual_rename = {k: v for k, v in rename_map.items() if k in ds.coords}
if actual_rename:
ds = ds.rename(actual_rename)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ArkVex just one last comment. These lines could just be reduced to

ds = ds.rename({k: v for k, v in rename_map.items() if k in ds.coords})

which will work even if an empty dictionary is input

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you go...I have implemented what you asked

Copy link
Member

@dfulu dfulu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ArkVex, looks good

@dfulu dfulu merged commit 21ffa3b into openclimatefix:main Feb 4, 2026
3 of 4 checks passed
@ArkVex
Copy link
Contributor Author

ArkVex commented Feb 4, 2026

@dfulu thankyou so much

@ArkVex
Copy link
Contributor Author

ArkVex commented Feb 4, 2026

@dfulu one thing i wanted to ask
When will the gsoc project list be released by ocf?

@dfulu
Copy link
Member

dfulu commented Feb 6, 2026

@ArkVex I don't know yet. Internally we're still only at the brainstorming stage for projects. So it will still take some time. If you haven't seen it already our community discussions page is the first place we'll announce the projects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support ukv variable name change

2 participants