-
-
Notifications
You must be signed in to change notification settings - Fork 40
Fetch remote FITS headers to provide accurate end times in e-Callisto search results #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AbhiLohar
wants to merge
2
commits into
sunpy:main
Choose a base branch
from
AbhiLohar:fix-ecallisto-metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+114
−8
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added remote FITS header fetching for the e-Callisto client to provide accurate start and end times in search results. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,10 @@ | |
| from sunpy.net.dataretriever.client import GenericClient | ||
|
|
||
| from radiospectra.net.attrs import Observatory | ||
|
|
||
| import zlib | ||
| import urllib.request | ||
| from astropy.io import fits | ||
| from astropy.time import Time | ||
|
|
||
| class eCALLISTOClient(GenericClient): | ||
| """ | ||
|
|
@@ -62,10 +65,28 @@ def post_search_hook(self, exdict, matchdict): | |
| original = super().post_search_hook(exdict, matchdict) | ||
| original["ID"] = original["suffix"].replace("_", "") | ||
| del original["suffix"] | ||
| # We don't know the end time for all files | ||
| # https://github.com/sunpy/radiospectra/issues/60 | ||
| del original["End Time"] | ||
| return original | ||
| url = exdict.get('url') | ||
| if url: | ||
| start_h, end_h = self._fetch_remote_header(url) | ||
| if start_h: | ||
| try: | ||
| original["Start Time"] = Time(start_h) | ||
| except Exception: | ||
| pass | ||
|
|
||
| if end_h: | ||
| try: | ||
| original["End Time"] = Time(end_h) | ||
| except Exception: | ||
| original["End Time"] = end_h | ||
| else: | ||
| if "End Time" in original: | ||
| del original["End Time"] | ||
| else: | ||
| if "End Time" in original: | ||
| del original["End Time"] | ||
|
|
||
| return original | ||
|
|
||
| @classmethod | ||
| def register_values(cls): | ||
|
|
@@ -97,3 +118,31 @@ def _can_handle_query(cls, *query): | |
| ): | ||
| return False | ||
| return True | ||
| def _fetch_remote_header(self, url): | ||
| headers = {'User-Agent': 'SunPy/Radiospectra', 'Range': 'bytes=0-15360'} | ||
| req = urllib.request.Request(url, headers=headers) | ||
| try: | ||
| with urllib.request.urlopen(req, timeout=5) as response: | ||
| if response.getcode() == 206: | ||
| compressed_data = response.read() | ||
| # e-Callisto files are .gz, so we need to decompress | ||
| d = zlib.decompressobj(16 + zlib.MAX_WBITS) | ||
| header_bytes = d.decompress(compressed_data) | ||
| header = fits.Header.fromstring(header_bytes[:2880].decode('ascii', errors='ignore')) | ||
|
|
||
| # Get Date and Time keywords | ||
| date_obs = header.get('DATE-OBS') | ||
| time_obs = header.get('TIME-OBS', '') | ||
| date_end = header.get('DATE-END', date_obs) | ||
| time_end = header.get('TIME-END', '') | ||
|
|
||
| # Combine into strings for Astropy Time | ||
| start = f"{date_obs} {time_obs}".strip() | ||
| end = f"{date_end} {time_end}".strip() | ||
|
|
||
| return (start if date_obs else None), (end if date_end else None) | ||
| except Exception: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very broad exception need to narrow down a bit to what you expect |
||
| return None, None | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should probably log or warn the user |
||
| return None, None | ||
|
|
||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need a test or two maybe one mocked and one against the real server?