-
-
Notifications
You must be signed in to change notification settings - Fork 412
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
Extend Gemini to include getting file content and logging out of GOA #2851
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
# Licensed under a 3-clause BSD style license - see LICENSE.rst | ||
""" | ||
Search functionality for the Gemini archive of observations. | ||
================================================== | ||
Gemini Observatory Archive (GOA) Astroquery Module | ||
================================================== | ||
|
||
For questions, contact [email protected] | ||
Query public and proprietary data from GOA. | ||
""" | ||
|
||
import os | ||
|
||
from datetime import date | ||
|
||
from astroquery import log | ||
import astropy | ||
from astropy import units | ||
from astropy.table import Table, MaskedColumn | ||
|
||
from astroquery.gemini.urlhelper import URLHelper | ||
import numpy as np | ||
|
||
from .urlhelper import URLHelper | ||
from ..query import QueryWithLogin | ||
from ..utils.class_or_instance import class_or_instance | ||
from . import conf | ||
|
@@ -433,6 +434,97 @@ | |
local_filepath = os.path.join(download_dir, filename) | ||
self._download_file(url=url, local_filepath=local_filepath, timeout=timeout) | ||
|
||
def _download_file_content(self, url, timeout=None, auth=None, method="GET", **kwargs): | ||
"""Download content from a URL and return it. Resembles | ||
`_download_file` but returns the content instead of saving it to a | ||
local file. | ||
|
||
Parameters | ||
---------- | ||
url : str | ||
The URL from where to download the file. | ||
timeout : int, optional | ||
Time in seconds to wait for the server response, by default | ||
`None`. | ||
auth : dict[str, Any], optional | ||
Authentication details, by default `None`. | ||
method : str, optional | ||
The HTTP method to use, by default "GET". | ||
|
||
Returns | ||
------- | ||
bytes | ||
The downloaded content. | ||
""" | ||
|
||
response = self._session.request(method, url, timeout=timeout, auth=auth, **kwargs) | ||
response.raise_for_status() | ||
|
||
if 'content-length' in response.headers: | ||
length = int(response.headers['content-length']) | ||
if length == 0: | ||
log.warn(f'URL {url} has length=0') | ||
|
||
blocksize = astropy.utils.data.conf.download_block_size | ||
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. you need to explicitly import this, importing |
||
content = b"" | ||
|
||
for block in response.iter_content(blocksize): | ||
content += block | ||
|
||
response.close() | ||
|
||
return content | ||
|
||
def logout(self): | ||
"""Logout from the GOA service by deleting the specific session cookie | ||
and updating the authentication state. | ||
""" | ||
# Delete specific cookie. | ||
cookie_name = "gemini_archive_session" | ||
if cookie_name in self._session.cookies: | ||
del self._session.cookies[cookie_name] | ||
|
||
# Update authentication state. | ||
self._authenticated = False | ||
|
||
def get_file_content(self, filename, timeout=None, auth=None, method="GET", **kwargs): | ||
"""Wrapper around `_download_file_content`. | ||
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. Use double backticks for everything that is not sphinx linkable. |
||
|
||
Parameters | ||
---------- | ||
filename : str | ||
Name of the file to download content. | ||
timeout : int, optional | ||
Time in seconds to wait for the server response, by default | ||
`None`. | ||
auth : dict[str, Any], optional | ||
Authentication details, by default `None`. | ||
method : str, optional | ||
The HTTP method to use, by default "GET". | ||
|
||
Returns | ||
------- | ||
bytes | ||
The downloaded content. | ||
""" | ||
url = self.get_file_url(filename) | ||
return self._download_file_content(url, timeout=timeout, auth=auth, method=method, **kwargs) | ||
|
||
def get_file_url(self, filename): | ||
"""Generate the file URL based on the filename. | ||
|
||
Parameters | ||
---------- | ||
filename : str | ||
The name of the file. | ||
|
||
Returns | ||
------- | ||
str | ||
The URL where the file can be downloaded. | ||
""" | ||
return f"https://archive.gemini.edu/file/{filename}" | ||
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. This url is hardwired at a lot of places, couldn't ve instead use the conf value, or store it as an instance attribute and use it here and elsewhere, too? 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. I agree. I have refactored the I did not want to make much changes on this first PR so I could first confirm that the documentation and coding styling matched |
||
|
||
|
||
def _gemini_json_to_table(json): | ||
""" | ||
|
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.
Make optional arguments kwarg only. Also, what are the possible keys in kwargs, explicitly list them if possible.
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.
The kwargs could be any accepted keys in the underlying
Session.request
. I can make a note of them.