Fix KeyError when accessing metadata for PSP RFS CDF files#187
Open
MohamedAli1937 wants to merge 4 commits intosunpy:mainfrom
Open
Fix KeyError when accessing metadata for PSP RFS CDF files#187MohamedAli1937 wants to merge 4 commits intosunpy:mainfrom
MohamedAli1937 wants to merge 4 commits intosunpy:mainfrom
Conversation
The SpectrogramFactory stored CDF global attributes under the key 'cdf_globals' in the metadata dict, but RFSSpectrogram.level and RFSSpectrogram.version read from 'cdf_meta'. This mismatch causes a KeyError when accessing spec.level or spec.version on spectrograms created from real CDF files. The existing tests use 'cdf_meta' in their mock metadata, so they pass despite the bug. Renamed all 4 occurrences in the factory to 'cdf_meta' for consistency.
samaloney
requested changes
Mar 27, 2026
Member
samaloney
left a comment
There was a problem hiding this comment.
Looks good but could you add a failing test using real data which this PR fixes?
Author
|
I added a real data test ( While doing this, I found a hidden bug: mock data used Fixes in this PR:
All tests pass locally, including the new real data test. To run the test: pytest --remote-data=any radiospectra/spectrogram/sources/tests/test_psp_rfs.pyPlease have a look @samaloney, thanks! |
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Description
Fixes #184
Summary:
This PR resolves a key mismatch between
SpectrogramFactoryand theRFSSpectrogramclass that causes aKeyErrorwhen attempting to access metadata properties on spectrograms loaded from real Parker Solar Probe (PSP RFS) CDF files.Motivation and Context:
Currently,
SpectrogramFactoryreads global attributes from CDF files usingcdfliband passes them into the final metadata dictionary under the key"cdf_globals".However, properties in
RFSSpectrogram(specificallylevelandversioninpsp_rfs.py) attempt to read that data usingself.meta["cdf_meta"].Because of this mismatch, downloading a real PSP RFS file via Fido and calling
spec.levelthrows an instant runtimeKeyError: 'cdf_meta'. (The existing unit tests pass only because they mock theparse_pathreturn value and manually inject mock metadata using the"cdf_meta"key.)Changes:
"cdf_globals"to"cdf_meta"inspectrogram_factory.py(for both PSP and SOLO RPW parsing blocks) to align the factory output with the keys expected by the source classes and the existing test suite.Architectural Justification: Why modify the Factory instead of
psp_rfs.py?It might intuitively seem safer to change
psp_rfs.pyto look for"cdf_globals". However, surgically modifyingspectrogram_factory.pyis the strictly correct choice for the following reasons:test_psp_rfs.py, andtest_solo_rpw.py) explicitly mocks metadata using the"cdf_meta"key."cdf_globals"was used exclusively inside the factory output.cdf_globalsrepresents the raw output from thecdflib.globalattsget()function. Renaming the final exported dictionary key to"cdf_meta"abstracts the data layer away from low-levelcdflibterminology, which is a cleaner architectural design.Note on Implementation: The local Python variables inside the factory function remain named
cdf_globals. This is intentional: they correctly describe the local return ofcdflib, and leaving them as-is minimizes unnecessary line changes (diff noise) in the PR. Only the exported dictionary key string was updated (i.e.,"cdf_meta": cdf_globals).AI Assistance Disclosure
AI tools were used for: