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

Add disable progress bar feature, edit docstrings #3280

Merged
merged 4 commits into from Aug 22, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/3280.feature.rst
@@ -0,0 +1 @@
Add ability to disable progressbars when dowloading files using `sunpy.net.helioviewer.py` and edited docstrings to mention this feature.
Cubostar marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 15 additions & 7 deletions sunpy/net/helioviewer.py
Expand Up @@ -128,7 +128,7 @@ def get_closest_image(self, date, observatory=None, instrument=None,

return response

def download_jp2(self, date, observatory=None, instrument=None, detector=None,
def download_jp2(self, date, progress=True, observatory=None, instrument=None, detector=None,
Cubostar marked this conversation as resolved.
Show resolved Hide resolved
measurement=None, source_id=None, directory=None, overwrite=False):
"""
Downloads the JPEG 2000 that most closely matches the specified time and
Expand All @@ -145,6 +145,10 @@ def download_jp2(self, date, observatory=None, instrument=None, detector=None,
----------
date : `astropy.time.Time`, `str`
A string or `~astropy.time.Time` object for the desired date of the image
progress : `bool`
Defaults to True.
If set to False, disables progress bars seen on terminal when
downloading files.
observatory : `str`
Observatory name
instrument : `str`
Expand All @@ -158,7 +162,7 @@ def download_jp2(self, date, observatory=None, instrument=None, detector=None,
This can be used directly instead of using the previous parameters.
directory : `str`
Directory to download JPEG 2000 image to.
overwrite : bool
overwrite : `bool`
Defaults to False.
If set to True, will overwrite any files with the same name.

Expand Down Expand Up @@ -188,7 +192,7 @@ def download_jp2(self, date, observatory=None, instrument=None, detector=None,
"sourceId": source_id,
}

return self._get_file(params, directory=directory, overwrite=overwrite)
return self._get_file(params, progress=progress, directory=directory, overwrite=overwrite)

def get_jp2_header(self, date, observatory=None, instrument=None, detector=None, measurement=None, jp2_id=None):
"""
Expand Down Expand Up @@ -249,7 +253,7 @@ def get_jp2_header(self, date, observatory=None, instrument=None, detector=None,
responses = responses.read().decode('utf-8')
return xml_to_dict(responses)['meta']

def download_png(self, date, image_scale, layers,
def download_png(self, date, image_scale, layers, progress=True,
directory=None, overwrite=False, watermark=False,
events="", event_labels=False,
scale=False, scale_type="earth", scale_x=0, scale_y=0,
Expand Down Expand Up @@ -291,6 +295,10 @@ def download_png(self, date, image_scale, layers,
Each layer string is comma-separated with either:
"[sourceId,visible,opacity]" or "[obs,inst,det,meas,visible,opacity]".
Multiple layers are: "[layer1],[layer2],[layer3]".
progress : `bool`, optional
Defaults to True.
If set to False, disables progress bars seen on terminal when
downloading files.
events : `str`, optional
Defaults to an empty string to indicate no feature/event annotations.
List feature/event types and FRMs to use to annoate the image.
Expand Down Expand Up @@ -390,7 +398,7 @@ def download_png(self, date, image_scale, layers,
"y1": y1, "y2": y2}
params.update(adict)

return self._get_file(params, directory=directory, overwrite=overwrite)
return self._get_file(params, progress=progress, directory=directory, overwrite=overwrite)

def is_online(self):
"""Returns True if Helioviewer is online and available."""
Expand All @@ -407,14 +415,14 @@ def _get_json(self, params):
response = self._request(params)
return json.load(reader(response))

def _get_file(self, params, directory=None, overwrite=False):
def _get_file(self, params, progress=True, directory=None, overwrite=False):
"""Downloads a file and return the filepath to that file."""
if directory is None:
directory = Path(sunpy.config.get('downloads', 'download_dir'))
else:
directory = Path(directory).expanduser().absolute()

downloader = parfive.Downloader(overwrite=overwrite)
downloader = parfive.Downloader(progress=progress, overwrite=overwrite)

url = urllib.parse.urljoin(self._api,
"?" + urllib.parse.urlencode(params))
Expand Down
32 changes: 32 additions & 0 deletions sunpy/net/tests/test_helioviewer.py
Expand Up @@ -141,3 +141,35 @@ def test_overwrite_png(self, client):
filepath_3 = client.download_png('2020/01/01', 2.4, "[SOHO,MDI,continuum,1,100]",
overwrite=True)
assert filepath_3 == filepath

def test_progress_jp2(self, client, capsys):
"""
Tests if progress bars are disabled when running `download_jp2()`.
"""
client = HelioviewerClient()
client.download_jp2("2012/01/01", observatory="SOHO",
instrument="MDI", measurement="continuum",
progress=False)
out, err = capsys.readouterr()
assert err == ""
client.download_jp2("2012/01/01", observatory="SOHO",
instrument="MDI", measurement="continuum")
out, err = capsys.readouterr()
assert err != ""

def test_progress_png(self, client, capsys):
"""
Tests if progress bars are disabled when running `download_png()`.
"""
client = HelioviewerClient()
client.download_png('2012/07/16 10:08:00', 2.4,
"[SDO,AIA,AIA,171,1,100]",
x0=0, y0=0, width=1024, height=1024,
progress=False)
out, err = capsys.readouterr()
assert err == ""
client.download_png('2012/07/16 10:08:00', 2.4,
"[SDO,AIA,AIA,171,1,100]",
x0=0, y0=0, width=1024, height=1024)
out, err = capsys.readouterr()
assert err != ""