-
Notifications
You must be signed in to change notification settings - Fork 0
DM-50087: Preload IERS data in Prompt Processing #328
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a7d331d
Document apdb_config directory in dev bucket.
kfindeisen a248e05
Replace bare asserts in tests with unittest assertions.
kfindeisen 5a78b80
Remove MockTestCase.
kfindeisen 3ec1b94
Add a script for exporting IERS data to S3.
kfindeisen 33b2ce8
Support multiple destinations in export_iers.py.
kfindeisen da6b101
Implement IERS cache update for Prompt Processing.
kfindeisen 2f1190b
Import the IERS cache on service/initializer start.
kfindeisen 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,67 @@ | ||
#!/usr/bin/env python | ||
# This file is part of prompt_processing. | ||
# | ||
# Developed for the LSST Data Management System. | ||
# This product includes software developed by the LSST Project | ||
# (https://www.lsst.org). | ||
# See the COPYRIGHT file at the top-level directory of this distribution | ||
# for details of code ownership. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
|
||
"""Export an IERS cache to a local or remote location. | ||
""" | ||
|
||
|
||
import argparse | ||
import logging | ||
import sys | ||
import tempfile | ||
|
||
import astropy.utils.data | ||
from astropy.utils import iers | ||
|
||
import lsst.resources | ||
|
||
|
||
IERS_URLS = [iers.IERS_A_URL, iers.IERS_B_URL, iers.IERS_LEAP_SECOND_URL] | ||
|
||
|
||
def _make_parser(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("destination", nargs="+", | ||
help="The path(s) or URI(s) to put the exported IERS cache. Should be a zip file.") | ||
return parser | ||
|
||
|
||
def main(): | ||
logging.basicConfig(level=logging.INFO, stream=sys.stdout) | ||
|
||
args = _make_parser().parse_args() | ||
# Update download cache | ||
for url in IERS_URLS: | ||
astropy.utils.data.download_file(url, cache="update") | ||
with tempfile.NamedTemporaryFile(suffix=".zip") as local_cache: | ||
astropy.utils.data.export_download_cache(local_cache, IERS_URLS, overwrite=True) | ||
src = lsst.resources.ResourcePath(local_cache.name, isTemporary=True) | ||
# Any errors past this point may invalidate the remote cache | ||
for d in args.destination: | ||
logging.info("Writing %s...", d) | ||
dest = lsst.resources.ResourcePath(d) | ||
dest.transfer_from(src, "copy", overwrite=True) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
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
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
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
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,60 @@ | ||
# This file is part of prompt_processing. | ||
# | ||
# Developed for the LSST Data Management System. | ||
# This product includes software developed by the LSST Project | ||
# (https://www.lsst.org). | ||
# See the COPYRIGHT file at the top-level directory of this distribution | ||
# for details of code ownership. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
__all__ = ["import_iers_cache"] | ||
|
||
|
||
import logging | ||
import os | ||
import tempfile | ||
|
||
import astropy.utils.data | ||
|
||
import lsst.resources | ||
import lsst.utils.timer | ||
|
||
|
||
_log = logging.getLogger("lsst." + __name__) | ||
_log.setLevel(logging.DEBUG) | ||
|
||
|
||
LOCAL_CACHE = os.path.join(tempfile.gettempdir(), "iers-cache.zip") | ||
|
||
|
||
def import_iers_cache(): | ||
"""Download the IERS cache from a shared resource at USDF. | ||
""" | ||
remote_cache = os.environ.get("CENTRAL_IERS_CACHE") | ||
if not os.path.exists(LOCAL_CACHE): | ||
if not remote_cache: | ||
_log.warning("No IERS download has been configured. Time conversions may be inaccurate.") | ||
return | ||
with lsst.utils.timer.time_this(_log, msg="Download IERS", level=logging.DEBUG): | ||
src = lsst.resources.ResourcePath(remote_cache) | ||
dest = lsst.resources.ResourcePath(LOCAL_CACHE) | ||
dest.transfer_from(src, "copy") | ||
else: | ||
_log.info("IERS cache already exists in this pod, skipping fresh download. " | ||
"This normally means that more than one worker has run in this pod.") | ||
|
||
with lsst.utils.timer.time_this(_log, msg="Update IERS", level=logging.DEBUG): | ||
astropy.utils.data.import_download_cache(LOCAL_CACHE, update_cache=True) | ||
_log.info("IERS cache is up to date.") |
This file was deleted.
Oops, something went wrong.
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,99 @@ | ||
# This file is part of prompt_processing. | ||
# | ||
# Developed for the LSST Data Management System. | ||
# This product includes software developed by the LSST Project | ||
# (https://www.lsst.org). | ||
# See the COPYRIGHT file at the top-level directory of this distribution | ||
# for details of code ownership. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
import os | ||
import tempfile | ||
import unittest | ||
import zipfile | ||
|
||
import boto3 | ||
|
||
from lsst.resources import ResourcePath, s3utils | ||
|
||
from shared.astropy import import_iers_cache, LOCAL_CACHE | ||
|
||
try: | ||
from moto import mock_aws | ||
except ImportError: | ||
from moto import mock_s3 as mock_aws # Backwards-compatible with moto 4 | ||
|
||
|
||
class IersCacheTest(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
cls.mock_aws = mock_aws() | ||
|
||
@classmethod | ||
def _make_iers_cache(cls, path): | ||
"""Create an IERS cache to use for testing. | ||
|
||
Parameters | ||
---------- | ||
path : `lsst.resources.ResourcePath` | ||
The location at which to create the cache. | ||
""" | ||
with tempfile.NamedTemporaryFile(mode="wb", suffix=".zip") as temp: | ||
with zipfile.ZipFile(temp, mode="w"): | ||
# Don't actually need any contents | ||
pass | ||
path.transfer_from(ResourcePath(temp.name), transfer="copy") | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.enterContext(s3utils.clean_test_environment_for_s3()) | ||
# Local cache should not exist before tests | ||
try: | ||
os.remove(LOCAL_CACHE) | ||
except FileNotFoundError: | ||
pass | ||
|
||
mock_endpoint = "https://this.is.a.test" | ||
self.bucket = "test-bucket-test" | ||
path = ResourcePath("test/test-cache.zip", root="s3://" + self.bucket) | ||
|
||
self.enterContext(unittest.mock.patch.dict( | ||
os.environ, | ||
{"MOTO_S3_CUSTOM_ENDPOINTS": mock_endpoint, | ||
"S3_ENDPOINT_URL": mock_endpoint, | ||
"CENTRAL_IERS_CACHE": path.geturl(), | ||
})) | ||
|
||
self.enterContext(self.mock_aws) | ||
s3 = boto3.resource("s3") | ||
s3.create_bucket(Bucket=self.bucket) | ||
|
||
self._make_iers_cache(path) | ||
|
||
def test_import_iers_cache(self): | ||
with unittest.mock.patch("astropy.utils.data.import_download_cache") as mock_import: | ||
import_iers_cache() | ||
mock_import.assert_called_once() | ||
self.assertEqual(mock_import.call_args.kwargs["update_cache"], True) | ||
|
||
def test_import_iers_cache_twice(self): | ||
with unittest.mock.patch("astropy.utils.data.import_download_cache") as mock_import: | ||
import_iers_cache() | ||
# Local cache should exist | ||
import_iers_cache() | ||
# Should import both times to ensure the latest version is used | ||
self.assertEqual(mock_import.call_count, 2) | ||
self.assertEqual(mock_import.call_args.kwargs["update_cache"], True) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.