Skip to content

Commit 64157c9

Browse files
committed
Simplifying code, and making sure both pixel and angular size works
1 parent 0e47ac0 commit 64157c9

File tree

2 files changed

+43
-32
lines changed

2 files changed

+43
-32
lines changed

astroquery/mast/cutouts.py

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -72,7 +72,7 @@ def _parse_cutout_size(size, timeout_add=None, mission=None):
7272
if np.isscalar(size):
7373
size = np.repeat(size, 2)
7474

75-
limit_reached = size[0] > 30 or size[1] > 30
75+
limit_reached = (size > 30).any()
7676

7777
if isinstance(size, u.Quantity):
7878
size = np.atleast_1d(size)
@@ -83,9 +83,8 @@ def _parse_cutout_size(size, timeout_add=None, mission=None):
8383
# Based on the literature, TESS resolution is approx. 21 arcseconds per pixel.
8484
# We will convert the recommended upper limit for a dimension from pixels
8585
# 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
86+
with u.set_enabled_equivalencies(u.pixel_scale(21 * u.arcsec / u.pixel)):
87+
limit_reached = (size > 30 * u.pixel).any()
8988

9089
if len(size) > 2:
9190
warnings.warn("Too many dimensions in cutout size, only the first two will be used.",
@@ -111,10 +110,10 @@ def _parse_cutout_size(size, timeout_add=None, mission=None):
111110
else:
112111
raise InvalidQueryError("Cutout size must be in pixels or angular quantity.")
113112

114-
if (mission == 'TESS') & (limit_reached) & (not timeout_add):
113+
if (mission == 'TESS') & (limit_reached) & (not timeout):
115114
warnings.warn("You have selected a large cutout size that may result in a timeout error. We suggest limiting"
116115
" 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)
116+
" default 600 seconds to something higher, using the timeout argument.", InputWarning)
118117

119118
return {"x": x, "y": y, "units": units}
120119

@@ -137,6 +136,11 @@ def __init__(self):
137136
}
138137
self._service_api_connection.set_service_params(services, "tesscut")
139138

139+
def _get_default_timeout(self):
140+
""" Gets the default request timeout limit. """
141+
142+
return self._service_api_connection.TIMEOUT
143+
140144
def get_sectors(self, *, coordinates=None, radius=0*u.deg, product='SPOC', objectname=None,
141145
moving_target=False, mt_type=None):
142146

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

257261
def download_cutouts(self, *, coordinates=None, size=5, sector=None, product='SPOC', path=".",
258262
inflate=True, objectname=None, moving_target=False, mt_type=None, verbose=False,
259-
timeout_add=None):
263+
timeout=None):
260264
"""
261265
Download cutout target pixel file(s) around the given coordinates with indicated size.
262266
@@ -313,12 +317,23 @@ def download_cutouts(self, *, coordinates=None, size=5, sector=None, product='SP
313317
first majorbody is tried and then smallbody if a matching majorbody is not found.
314318
315319
NOTE: If moving_target is supplied, this argument is ignored.
320+
timeout : int or float, optional
321+
The modified request timeout limit.
322+
The request processing time by default is 600 seconds, meaning an attempt at communicating
323+
with the API will take 600 seconds before timing out. The timeout upper limit can be modified
324+
using this argument for large cutout requests via TESSCut. Default is None.
316325
317326
Returns
318327
-------
319328
response : `~astropy.table.Table`
320329
"""
321330

331+
# Modify TIMEOUT attribute if necessary (usually this is modified for large requests)
332+
if timeout:
333+
self._service_api_connection.TIMEOUT = timeout
334+
log.info(f"Request timeout upper limit is being changed to {self._service_api_connection.TIMEOUT}"
335+
" seconds.")
336+
322337
if moving_target:
323338

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

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

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

391406
localpath_table['Local Path'] = [path+x for x in cutout_files]
407+
408+
if timeout:
409+
self._service_api_connection.TIMEOUT = self._get_default_timeout()
410+
392411
return localpath_table
393412

394413
def get_cutouts(self, *, coordinates=None, size=5, product='SPOC', sector=None,
395-
objectname=None, moving_target=False, mt_type=None, timeout_add=None):
414+
objectname=None, moving_target=False, mt_type=None, timeout=None):
396415
"""
397416
Get cutout target pixel file(s) around the given coordinates with indicated size,
398417
and return them as a list of `~astropy.io.fits.HDUList` objects.
@@ -441,8 +460,8 @@ def get_cutouts(self, *, coordinates=None, size=5, product='SPOC', sector=None,
441460
first majorbody is tried and then smallbody if a matching majorbody is not found.
442461
443462
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.
463+
timeout : int or float, optional
464+
The modified request timeout limit.
446465
The request processing time by default is 600 seconds, meaning an attempt at communicating
447466
with the API will take 600 seconds before timing out. The timeout upper limit can be modified
448467
using this argument for large cutout requests via TESSCut. Default is None.
@@ -453,13 +472,13 @@ def get_cutouts(self, *, coordinates=None, size=5, product='SPOC', sector=None,
453472
"""
454473

455474
# 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
475+
if timeout:
476+
self._service_api_connection.TIMEOUT = timeout
458477
log.info(f"Request timeout upper limit is being changed to {self._service_api_connection.TIMEOUT}"
459478
" seconds.")
460479

461480
# Setting up the cutout size
462-
param_dict = _parse_cutout_size(size, timeout_add=timeout_add, mission='TESS')
481+
param_dict = _parse_cutout_size(size, timeout=timeout, mission='TESS')
463482

464483
# Add sector if present
465484
if sector:
@@ -529,6 +548,9 @@ def get_cutouts(self, *, coordinates=None, size=5, product='SPOC', sector=None,
529548
# preserve the original filename in the fits object
530549
cutout_hdus_list[-1].filename = name
531550

551+
if timeout:
552+
self._service_api_connection.TIMEOUT = self._get_default_timeout()
553+
532554
return cutout_hdus_list
533555

534556

@@ -592,7 +614,7 @@ def get_surveys(self, coordinates, *, radius="0d"):
592614
return survey_json
593615

594616
def download_cutouts(self, coordinates, *, size=5, survey=None, cutout_format="fits", path=".", inflate=True,
595-
verbose=False, timeout_add=None, **img_params):
617+
verbose=False, **img_params):
596618
"""
597619
Download cutout FITS/image file(s) around the given coordinates with indicated size.
598620
@@ -633,24 +655,13 @@ def download_cutouts(self, coordinates, *, size=5, survey=None, cutout_format="f
633655
The Column Name is the keyword, with the argument being one or more acceptable
634656
values for that parameter, except for fields with a float datatype where the
635657
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.
641658
642659
Returns
643660
-------
644661
response : `~astropy.table.Table`
645662
Cutout file(s) for given coordinates
646663
"""
647664

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-
654665
# Get Skycoord object for coordinates/object
655666
coordinates = parse_input_location(coordinates)
656667
size_dict = _parse_cutout_size(size)

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)