Skip to content

Commit 1cdf7c1

Browse files
jaymedinabsipocz
authored andcommitted
Simplifying code, updating unit tests, error prevention
1 parent 7ba24ce commit 1cdf7c1

File tree

4 files changed

+75
-36
lines changed

4 files changed

+75
-36
lines changed

CHANGES.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ nist
156156

157157
- Vectoized ``linename`` option to query multiple spectral lines with one call
158158
of ``Nist.query``. [#2678]
159-
``TesscutClass``. [#2668]
160159

161160
oac
162161
^^^

astroquery/mast/cutouts.py

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from astropy.io import fits
2727

2828
from .. import log
29-
from ..exceptions import InputWarning, NoResultsWarning, InvalidQueryError
29+
from ..exceptions import InputWarning, LargeQueryWarning, NoResultsWarning, InvalidQueryError
3030

3131
from .utils import parse_input_location
3232
from .core import MastQueryWithLogin
@@ -35,7 +35,7 @@
3535
__all__ = ["TesscutClass", "Tesscut", "ZcutClass", "Zcut"]
3636

3737

38-
def _parse_cutout_size(size, timeout_add=None, mission=None):
38+
def _parse_cutout_size(size, timeout=None, mission=None):
3939
"""
4040
Take a user input cutout size and parse it into the regular format
4141
[ny,nx] where nx/ny are quantities with units either pixels or degrees.
@@ -53,8 +53,8 @@ def _parse_cutout_size(size, timeout_add=None, mission=None):
5353
The mission for which the size parsing is being done. This parameter
5454
is mainly meant to trigger a cutout size warning specifically for TESSCut
5555
requests. Default is None.
56-
timeout_add : int or float, optional
57-
The amount (in seconds) by which the request processing time upper limit will be changed.
56+
timeout : int or float, optional
57+
The modified request timeout limit.
5858
The request processing time by default is 600 seconds, meaning an attempt at communicating
5959
with the API will take 600 seconds before timing out. In the context of this function, this
6060
parameter is meant to keep track of whether or not the timeout limit has been modified, which
@@ -68,11 +68,15 @@ def _parse_cutout_size(size, timeout_add=None, mission=None):
6868
either pixels or degrees.
6969
"""
7070

71+
# This local variable will change to True if input cutout size exceeds recommended limits for TESS
72+
limit_reached = False
73+
7174
# Making size into an array [ny, nx]
7275
if np.isscalar(size):
7376
size = np.repeat(size, 2)
7477

75-
limit_reached = size[0] > 30 or size[1] > 30
78+
if mission == 'TESS':
79+
limit_reached = (size > 30).any()
7680

7781
if isinstance(size, u.Quantity):
7882
size = np.atleast_1d(size)
@@ -83,14 +87,24 @@ def _parse_cutout_size(size, timeout_add=None, mission=None):
8387
# Based on the literature, TESS resolution is approx. 21 arcseconds per pixel.
8488
# We will convert the recommended upper limit for a dimension from pixels
8589
# to the unit being passed.
86-
unit = size[0].unit
87-
upper_limit = (30 * 21*u.arcsec).to(unit).value
88-
limit_reached = size[0].value > upper_limit or size[1].value > upper_limit
90+
if mission == 'TESS':
91+
with u.set_enabled_equivalencies(u.pixel_scale(21 * u.arcsec / u.pixel)):
92+
limit_reached = (size > 30 * u.pixel).any()
8993

9094
if len(size) > 2:
9195
warnings.warn("Too many dimensions in cutout size, only the first two will be used.",
9296
InputWarning)
93-
97+
98+
# Checking 2d size inputs for the recommended cutout size
99+
if (len(size) == 2) & (mission == 'TESS'):
100+
101+
if np.isscalar(size[0]):
102+
print(size)
103+
size = size * u.pixel
104+
105+
with u.set_enabled_equivalencies(u.pixel_scale(21 * u.arcsec / u.pixel)):
106+
limit_reached = (size * size[0].unit > 30 * u.pixel).any()
107+
94108
# Getting x and y out of the size
95109

96110
if np.isscalar(size[0]):
@@ -111,10 +125,10 @@ def _parse_cutout_size(size, timeout_add=None, mission=None):
111125
else:
112126
raise InvalidQueryError("Cutout size must be in pixels or angular quantity.")
113127

114-
if (mission == 'TESS') & (limit_reached) & (not timeout_add):
128+
if (limit_reached) & (not timeout):
115129
warnings.warn("You have selected a large cutout size that may result in a timeout error. We suggest limiting"
116130
" the size of your requested cutout, or changing the request timeout limit from its"
117-
" default 600 seconds to something higher, using the timeout_add argument.", InputWarning)
131+
" default 600 seconds to something higher, using the timeout argument.", LargeQueryWarning)
118132

119133
return {"x": x, "y": y, "units": units}
120134

@@ -137,6 +151,11 @@ def __init__(self):
137151
}
138152
self._service_api_connection.set_service_params(services, "tesscut")
139153

154+
def _get_default_timeout(self):
155+
""" Gets the default request timeout limit. """
156+
157+
return self._service_api_connection.TIMEOUT
158+
140159
def get_sectors(self, *, coordinates=None, radius=0*u.deg, product='SPOC', objectname=None,
141160
moving_target=False, mt_type=None):
142161

@@ -256,7 +275,7 @@ def get_sectors(self, *, coordinates=None, radius=0*u.deg, product='SPOC', objec
256275

257276
def download_cutouts(self, *, coordinates=None, size=5, sector=None, product='SPOC', path=".",
258277
inflate=True, objectname=None, moving_target=False, mt_type=None, verbose=False,
259-
timeout_add=None):
278+
timeout=None):
260279
"""
261280
Download cutout target pixel file(s) around the given coordinates with indicated size.
262281
@@ -313,12 +332,23 @@ def download_cutouts(self, *, coordinates=None, size=5, sector=None, product='SP
313332
first majorbody is tried and then smallbody if a matching majorbody is not found.
314333
315334
NOTE: If moving_target is supplied, this argument is ignored.
335+
timeout : int or float, optional
336+
The modified request timeout limit.
337+
The request processing time by default is 600 seconds, meaning an attempt at communicating
338+
with the API will take 600 seconds before timing out. The timeout upper limit can be modified
339+
using this argument for large cutout requests via TESSCut. Default is None.
316340
317341
Returns
318342
-------
319343
response : `~astropy.table.Table`
320344
"""
321345

346+
# Modify TIMEOUT attribute if necessary (usually this is modified for large requests)
347+
if timeout:
348+
self._service_api_connection.TIMEOUT = timeout
349+
log.info(f"Request timeout upper limit is being changed to {self._service_api_connection.TIMEOUT}"
350+
" seconds.")
351+
322352
if moving_target:
323353

324354
# The Moving Targets service is currently only available for SPOC
@@ -348,7 +378,7 @@ def download_cutouts(self, *, coordinates=None, size=5, sector=None, product='SP
348378
astrocut_request = f"astrocut?ra={coordinates.ra.deg}&dec={coordinates.dec.deg}"
349379

350380
# Adding the arguments that are common between moving/still astrocut requests
351-
size_dict = _parse_cutout_size(size, timeout_add=timeout_add, mission='TESS')
381+
size_dict = _parse_cutout_size(size, timeout=timeout, mission='TESS')
352382
astrocut_request += f"&y={size_dict['y']}&x={size_dict['x']}&units={size_dict['units']}"
353383

354384
# Making sure input product is either SPOC or TICA,
@@ -389,10 +419,14 @@ def download_cutouts(self, *, coordinates=None, size=5, sector=None, product='SP
389419
os.remove(zipfile_path)
390420

391421
localpath_table['Local Path'] = [path+x for x in cutout_files]
422+
423+
if timeout:
424+
self._service_api_connection.TIMEOUT = self._get_default_timeout()
425+
392426
return localpath_table
393427

394428
def get_cutouts(self, *, coordinates=None, size=5, product='SPOC', sector=None,
395-
objectname=None, moving_target=False, mt_type=None, timeout_add=None):
429+
objectname=None, moving_target=False, mt_type=None, timeout=None):
396430
"""
397431
Get cutout target pixel file(s) around the given coordinates with indicated size,
398432
and return them as a list of `~astropy.io.fits.HDUList` objects.
@@ -441,8 +475,8 @@ def get_cutouts(self, *, coordinates=None, size=5, product='SPOC', sector=None,
441475
first majorbody is tried and then smallbody if a matching majorbody is not found.
442476
443477
NOTE: If moving_target is supplied, this argument is ignored.
444-
timeout_add : int or float, optional
445-
The amount (in seconds) by which the request processing time upper limit will be changed.
478+
timeout : int or float, optional
479+
The modified request timeout limit.
446480
The request processing time by default is 600 seconds, meaning an attempt at communicating
447481
with the API will take 600 seconds before timing out. The timeout upper limit can be modified
448482
using this argument for large cutout requests via TESSCut. Default is None.
@@ -453,13 +487,13 @@ def get_cutouts(self, *, coordinates=None, size=5, product='SPOC', sector=None,
453487
"""
454488

455489
# Modify TIMEOUT attribute if necessary (usually this is modified for large requests)
456-
if timeout_add:
457-
self._service_api_connection.TIMEOUT = self._service_api_connection.TIMEOUT + timeout_add
490+
if timeout:
491+
self._service_api_connection.TIMEOUT = timeout
458492
log.info(f"Request timeout upper limit is being changed to {self._service_api_connection.TIMEOUT}"
459493
" seconds.")
460494

461495
# Setting up the cutout size
462-
param_dict = _parse_cutout_size(size, timeout_add=timeout_add, mission='TESS')
496+
param_dict = _parse_cutout_size(size, timeout=timeout, mission='TESS')
463497

464498
# Add sector if present
465499
if sector:
@@ -529,6 +563,9 @@ def get_cutouts(self, *, coordinates=None, size=5, product='SPOC', sector=None,
529563
# preserve the original filename in the fits object
530564
cutout_hdus_list[-1].filename = name
531565

566+
if timeout:
567+
self._service_api_connection.TIMEOUT = self._get_default_timeout()
568+
532569
return cutout_hdus_list
533570

534571

@@ -592,7 +629,7 @@ def get_surveys(self, coordinates, *, radius="0d"):
592629
return survey_json
593630

594631
def download_cutouts(self, coordinates, *, size=5, survey=None, cutout_format="fits", path=".", inflate=True,
595-
verbose=False, timeout_add=None, **img_params):
632+
verbose=False, **img_params):
596633
"""
597634
Download cutout FITS/image file(s) around the given coordinates with indicated size.
598635
@@ -633,24 +670,13 @@ def download_cutouts(self, coordinates, *, size=5, survey=None, cutout_format="f
633670
The Column Name is the keyword, with the argument being one or more acceptable
634671
values for that parameter, except for fields with a float datatype where the
635672
argument should be in the form [minVal, maxVal].
636-
timeout_add : int or float, optional
637-
The amount (in seconds) by which the request processing time upper limit will be changed.
638-
The request processing time by default is 600 seconds, meaning an attempt at communicating
639-
with the API will take 600 seconds before timing out. The timeout upper limit can be modified
640-
using this argument for large cutout requests via TESSCut. Default is None.
641673
642674
Returns
643675
-------
644676
response : `~astropy.table.Table`
645677
Cutout file(s) for given coordinates
646678
"""
647679

648-
# Modify TIMEOUT attribute if necessary (usually this is modified for large requests)
649-
if timeout_add:
650-
self._service_api_connection.TIMEOUT = self._service_api_connection.TIMEOUT + timeout_add
651-
log.info(f"Request timeout upper limit is being changed to {self._service_api_connection.TIMEOUT}"
652-
" seconds.")
653-
654680
# Get Skycoord object for coordinates/object
655681
coordinates = parse_input_location(coordinates)
656682
size_dict = _parse_cutout_size(size)

astroquery/mast/tests/test_mast_remote.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ def test_tesscut_download_cutouts_mt(self, tmpdir):
949949
assert error_tica_mt in str(error_msg.value)
950950

951951
@pytest.mark.parametrize("product", ["tica", "spoc"])
952-
def test_tesscut_get_cutouts(self, product):
952+
def test_tesscut_get_cutouts(self, product, caplog):
953953

954954
coord = SkyCoord(107.18696, -70.50919, unit="deg")
955955

@@ -975,6 +975,11 @@ def test_tesscut_get_cutouts(self, product):
975975
assert len(cutout_hdus_list) >= 1
976976
assert isinstance(cutout_hdus_list[0], fits.HDUList)
977977

978+
# Check that an INFO message is returned when timeout is adjusted
979+
mast.Tesscut.get_cutouts(product=product, coordinates=coord, size=5, timeout=1000)
980+
with caplog.at_level("INFO", logger="astroquery"):
981+
assert "timeout upper limit is being changed" in caplog.text
982+
978983
def test_tesscut_get_cutouts_mt(self):
979984

980985
# Moving target functionality testing
@@ -1027,6 +1032,15 @@ def test_tesscut_get_cutouts_mt(self):
10271032
moving_target=True)
10281033
assert error_tica_mt in str(error_msg.value)
10291034

1035+
@pytest.mark.xfail(raises=InputWarning)
1036+
@pytest.mark.parametrize("product", ["tica", "spoc"])
1037+
@pytest.mark.parametrize("size", [31, 0.2 * u.deg, 5000 * u.arcsec, 20 * u.arcmin])
1038+
def test_tesscut_timeout_param(self, product, size):
1039+
1040+
# Check that a warning comes up when cutout size too big
1041+
coordinates = '60 60'
1042+
mast.Tesscut.get_cutouts(product=product, coordinates=coordinates, size=size)
1043+
10301044
###################
10311045
# ZcutClass tests #
10321046
###################

docs/mast/mast.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,10 +1060,10 @@ around 34 x 34 pixels-squared.
10601060
>>> hdulist = Tesscut.get_cutouts(coordinates=cutout_coord, size=0.2*u.deg)
10611061
WARNING: InputWarning: You have selected a large cutout size that may result in a timeout error.
10621062
We suggest limiting the size of your requested cutout, or changing the request timeout limit from
1063-
its default 600 seconds to something higher, using the timeout_add argument. [astroquery.mast.cutouts]
1063+
its default 600 seconds to something higher, using the timeout argument. [astroquery.mast.cutouts]
10641064

10651065
At this point, users may choose to decrease their cutout size or extend the request timeout limit from
1066-
600 seconds to something longer. Using the example above, we will use the ``timeout_add`` argument to extend
1066+
600 seconds to something longer. Using the example above, we will use the ``timeout`` argument to change
10671067
the request timeout limit from 600 seconds to 2600 seconds, or approximately 43 minutes.
10681068

10691069
.. doctest-remote-data::
@@ -1073,7 +1073,7 @@ the request timeout limit from 600 seconds to 2600 seconds, or approximately 43
10731073
>>> from astropy.coordinates import SkyCoord
10741074
...
10751075
>>> cutout_coord = SkyCoord(107.18696, -70.50919, unit="deg")
1076-
>>> hdulist = Tesscut.get_cutouts(coordinates=cutout_coord, size=0.2*u.deg, timeout_add=2000)
1076+
>>> hdulist = Tesscut.get_cutouts(coordinates=cutout_coord, size=0.2*u.deg, timeout=2600)
10771077
INFO: Request timeout upper limit is being changed to 2600 seconds. [astroquery.mast.cutouts]
10781078

10791079
Sector information

0 commit comments

Comments
 (0)