Skip to content
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

Download methods to FileManager for quality and movie #235

Merged
merged 3 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/235.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add new methods `.FileManager.quality_report()` and `.FileManager.preview_movie()` to download the quality report and preview movie. These are accessed as ``Dataset.files.quality_report`` and ``Dataset.files.preview_movie``.
2 changes: 1 addition & 1 deletion dkist/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def from_directory(cls, directory):
elif len(asdf_files) > 1:
raise NotImplementedError("Multiple asdf files found in this "
"directory. Use from_asdf to specify which "
"one to use.") # pragma: no cover
"one to use.") # pragma: no cover

asdf_file = asdf_files[0]

Expand Down
68 changes: 66 additions & 2 deletions dkist/io/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import dask.array
import numpy as np
from parfive import Downloader

try:
from numpy.typing import DTypeLike, NDArray # NOQA
Expand Down Expand Up @@ -314,6 +315,69 @@ def __init__(self, fits_loader: StripedExternalArray):
# be populated with a reference to that Dataset instance.
self._ndcube = None

@property
def _metadata_streamer_url(self):
# Import here to avoid circular import
from dkist.net import conf

return conf.download_endpoint

def quality_report(self, path=None, overwrite=None):
"""
Download the quality report PDF.

Parameters
----------
path: `str` or `pathlib.Path`
The destination path to save the file to. See
`parfive.Downloader.simple_download` for details.
The default path is ``.basepath``, if ``.basepath`` is None it will
default to `~/`.
overwrite: `bool`
Set to `True` to overwrite file if it already exists. See
`parfive.Downloader.simple_download` for details.

Returns
-------
results: `parfive.Results`
A `~parfive.Results` obejct containing the filepath of the
downloaded file if the download was successful, and any errors if it
was not.
"""
dataset_id = self._ndcube.meta['inventory']['datasetId']
url = f"{self._metadata_streamer_url}/quality?datasetId={dataset_id}"
if path is None and self.basepath:
path = self.basepath
return Downloader.simple_download([url], path=path, overwrite=overwrite)

def preview_movie(self, path=None, overwrite=None):
"""
Download the preview movie.

Parameters
----------
path: `str` or `pathlib.Path`
The destination path to save the file to. See
`parfive.Downloader.simple_download` for details.
The default path is ``.basepath``, if ``.basepath`` is None it will
default to `~/`.
overwrite: `bool`
Set to `True` to overwrite file if it already exists. See
`parfive.Downloader.simple_download` for details.

Returns
-------
results: `parfive.Results`
A `~parfive.Results` obejct containing the filepath of the
downloaded file if the download was successful, and any errors if it
was not.
"""
dataset_id = self._ndcube.meta['inventory']['datasetId']
url = f"{self._metadata_streamer_url}/movie?datasetId={dataset_id}"
if path is None and self.basepath:
path = self.basepath
return Downloader.simple_download([url], path=path, overwrite=overwrite)

def download(self, path=None, destination_endpoint=None, progress=True, wait=True, label=None):
"""
Start a Globus file transfer for all files in this Dataset.
Expand All @@ -323,8 +387,8 @@ def download(self, path=None, destination_endpoint=None, progress=True, wait=Tru
path : `pathlib.Path` or `str`, optional
The path to save the data in, must be accessible by the Globus
endpoint.
The default value is ``.basepath``, if this is None it will default
to ``/~/``.
The default value is ``.basepath``, if ``.basepath`` is None it will
default to ``/~/``.
It is possible to put placeholder strings in the path with any key
from the dataset inventory dictionary which can be accessed as
``ds.meta['inventory']``. An example of this would be
Expand Down
44 changes: 44 additions & 0 deletions dkist/io/tests/test_file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,47 @@ def test_length_one_first_array_axis(small_visp_dataset):
assert len(small_visp_dataset[0].files.filenames) == 1

assert len(small_visp_dataset[:, 5, 5].files.filenames) == 3


@pytest.mark.parametrize("kwargs", (
{},
{"path": "~/", "overwrite": True}
))
def test_download_quality(mocker, small_visp_dataset, kwargs):
simple_download = mocker.patch("dkist.io.file_manager.Downloader.simple_download")
from dkist.net import conf

small_visp_dataset.files.quality_report(**kwargs)

# Insert the expected default kwargs
if kwargs.get("path") is None:
kwargs["path"] = small_visp_dataset.files.basepath
if "overwrite" not in kwargs:
kwargs["overwrite"] = None

simple_download.assert_called_once_with(
[f"{conf.download_endpoint}/quality?datasetId={small_visp_dataset.meta['inventory']['datasetId']}"],
**kwargs
)


@pytest.mark.parametrize("kwargs", (
{},
{"path": "~/", "overwrite": True}
))
def test_download_quality_movie(mocker, small_visp_dataset, kwargs):
simple_download = mocker.patch("dkist.io.file_manager.Downloader.simple_download")
from dkist.net import conf

small_visp_dataset.files.preview_movie(**kwargs)

# Insert the expected default kwargs
if kwargs.get("path") is None:
kwargs["path"] = small_visp_dataset.files.basepath
if "overwrite" not in kwargs:
kwargs["overwrite"] = None

simple_download.assert_called_once_with(
[f"{conf.download_endpoint}/movie?datasetId={small_visp_dataset.meta['inventory']['datasetId']}"],
**kwargs
)