Skip to content

Commit d09efd7

Browse files
committed
Tidy up as recommended by review
1 parent 37dfb93 commit d09efd7

File tree

2 files changed

+52
-45
lines changed

2 files changed

+52
-45
lines changed

astroquery/casda/core.py

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(self, user=None, password=None):
5858
# self._password = password
5959
self._auth = (user, password)
6060

61-
def query_region_async(self, coordinates, radius=None, height=None, width=None,
61+
def query_region_async(self, coordinates, radius=1*u.arcmin, height=None, width=None,
6262
get_query_payload=False, cache=True):
6363
"""
6464
Queries a region around the specified coordinates. Either a radius or both a height and a width must be provided.
@@ -97,62 +97,62 @@ def query_region_async(self, coordinates, radius=None, height=None, width=None,
9797

9898
# Create the dict of HTTP request parameters by parsing the user
9999
# entered values.
100-
def _args_to_payload(self, **kwargs):
100+
def _args_to_payload(self, radius=1*u.arcmin, **kwargs):
101101
request_payload = dict()
102102

103103
# Convert the coordinates to FK5
104104
coordinates = kwargs.get('coordinates')
105105
if coordinates is not None:
106106
fk5_coords = commons.parse_coordinates(coordinates).transform_to(coord.FK5)
107107

108-
if kwargs.get('radius') is not None:
109-
radius = u.Quantity(kwargs['radius']).to(u.deg)
110-
pos = 'CIRCLE {} {} {}'.format(fk5_coords.ra.degree, fk5_coords.dec.degree, radius.value)
111-
elif kwargs.get('width') is not None and kwargs.get('height') is not None:
108+
if kwargs.get('width') is not None and kwargs.get('height') is not None:
112109
width = u.Quantity(kwargs['width']).to(u.deg).value
113110
height = u.Quantity(kwargs['height']).to(u.deg).value
114111
top = fk5_coords.dec.degree + (height/2)
115112
bottom = fk5_coords.dec.degree - (height/2)
116113
left = fk5_coords.ra.degree - (width/2)
117114
right = fk5_coords.ra.degree + (width/2)
118-
pos = 'RANGE {} {} {} {}'.format(left, right, bottom, top)
115+
pos = f'RANGE {left} {right} {bottom} {top}'
119116
else:
120-
pos = 'CIRCLE {} {} {}'.format(fk5_coords.ra.degree, fk5_coords.dec.degree, 1*u.arcmin.to(u.deg))
117+
radius = u.Quantity(radius).to(u.deg)
118+
pos = f'CIRCLE {fk5_coords.ra.degree} {fk5_coords.dec.degree} {radius.value}'
121119

122120
request_payload['POS'] = pos
123121

124-
if kwargs.get('band') is not None:
125-
if kwargs.get('channel') is not None:
122+
band = kwargs.get('band')
123+
channel = kwargs.get('channel')
124+
if band is not None:
125+
if channel is not None:
126126
raise ValueError("Either 'channel' or 'band' values may be provided but not both.")
127127

128-
band = kwargs.get('band')
129-
if not isinstance(band, (list, tuple)) or len(band) != 2:
128+
if (not isinstance(band, (list, tuple))) or len(band) != 2 or \
129+
(band[0] is not None and not isinstance(band[0], u.Quantity)) or \
130+
(band[1] is not None and not isinstance(band[1], u.Quantity)):
130131
raise ValueError("The 'band' value must be a list of 2 wavelength or frequency values.")
131-
if (band[0] is not None and not isinstance(band[0], u.Quantity)) or (band[1] is not None and not isinstance(band[1], u.Quantity)):
132-
raise ValueError("The 'band' value must be a list of 2 wavelength or frequency values.")
133-
if band[0] is not None and band[1] is not None and band[0].unit.physical_type != band[1].unit.physical_type:
132+
133+
bandBoundedLow = band[0] is not None
134+
bandBoundedHigh = band[1] is not None
135+
if bandBoundedLow and bandBoundedHigh and band[0].unit.physical_type != band[1].unit.physical_type:
134136
raise ValueError("The 'band' values must have the same kind of units.")
135-
if band[0] is not None or band[1] is not None:
136-
unit = band[0].unit if band[0] is not None else band[1].unit
137+
if bandBoundedLow or bandBoundedHigh:
138+
unit = band[0].unit if bandBoundedLow else band[1].unit
137139
if unit.physical_type == 'length':
138-
min_band = '-Inf' if band[0] is None else str(band[0].to(u.m).value)
139-
max_band = '+Inf' if band[1] is None else str(band[1].to(u.m).value)
140+
min_band = '-Inf' if not bandBoundedLow else str(band[0].to(u.m).value)
141+
max_band = '+Inf' if not bandBoundedHigh else str(band[1].to(u.m).value)
140142
elif unit.physical_type == 'frequency':
141143
# Swap the order when changing frequency to wavelength
142-
min_band = '-Inf' if band[1] is None else str(band[1].to(u.m, equivalencies=u.spectral()).value)
143-
max_band = '+Inf' if band[0] is None else str(band[0].to(u.m, equivalencies=u.spectral()).value)
144+
min_band = '-Inf' if not bandBoundedHigh else str(band[1].to(u.m, equivalencies=u.spectral()).value)
145+
max_band = '+Inf' if not bandBoundedLow else str(band[0].to(u.m, equivalencies=u.spectral()).value)
144146
else:
145147
raise ValueError("The 'band' values must be wavelengths or frequencies.")
146148

147-
request_payload['BAND'] = '{} {}'.format(min_band, max_band)
149+
request_payload['BAND'] = f'{min_band} {max_band}'
148150

149-
if kwargs.get('channel') is not None:
150-
channel = kwargs.get('channel')
151-
if not isinstance(channel, (list, tuple)) or len(channel) != 2:
152-
raise ValueError("The 'channel' value must be a list of 2 integer values.")
153-
if (not isinstance(channel[0], int)) or (not isinstance(channel[1], int)):
151+
if channel is not None:
152+
if not isinstance(channel, (list, tuple)) or len(channel) != 2 or \
153+
not isinstance(channel[0], int) or not isinstance(channel[1], int):
154154
raise ValueError("The 'channel' value must be a list of 2 integer values.")
155-
request_payload['CHANNEL'] = '{} {}'.format(channel[0], channel[1])
155+
request_payload['CHANNEL'] = f'{channel[0]} {channel[1]}'
156156

157157
return request_payload
158158

@@ -264,32 +264,34 @@ def stage_data(self, table, verbose=False):
264264

265265
return self._complete_job(job_url, verbose)
266266

267-
def cutout(self, table, coordinates=None, radius=None, height=None, width=None, band=None, channel=None, verbose=False):
267+
def cutout(self, table, radius=1*u.arcmin, verbose=False, **kwargs):
268268
"""
269269
Produce a cutout from each selected file. All requests for data must use authentication. If you have access to
270270
the data, the requested files will be brought online, a cutout produced from each file and a set of URLs to
271271
download the cutouts will be returned.
272272
273-
If a set of coordinates is provided along with either a radius or a box height and width, then CASDA will produce a
274-
spatial cutout at that location from each data file specified in the table. If a band or channel pair is provided
275-
then CASDA will produce a spectral cutout of that range from each data file. These can be combined to produce
276-
subcubes with restrictions in both spectral and spatial axes.
273+
If a set of coordinates is provided along with either a radius or a box height and width, then CASDA will
274+
produce a spatial cutout at that location from each data file specified in the table. If a band or channel pair
275+
is provided then CASDA will produce a spectral cutout of that range from each data file. These can be combined
276+
to produce subcubes with restrictions in both spectral and spatial axes.
277277
278278
Parameters
279279
----------
280280
table: `astropy.table.Table`
281281
A table describing the files to be staged, such as produced by query_region. It must include an
282282
access_url column.
283283
coordinates : str or `astropy.coordinates`, optional
284-
coordinates around which to produce a cutout, the radius will be 1 arcmin if no radius, height or width is provided.
284+
coordinates around which to produce a cutout, the radius will be 1 arcmin if no radius, height or width is
285+
provided.
285286
radius : str or `astropy.units.Quantity`, optional
286287
the radius of the cutout
287288
height : str or `astropy.units.Quantity`, optional
288289
the height for a box cutout
289290
width : str or `astropy.units.Quantity`, optional
290291
the width for a box cutout
291292
band : list of `astropy.units.Quantity` with two elements, optional
292-
the spectral range to be included, may be low and high wavelengths in metres or low and high frequencies in Hertz. Use None for an open bound.
293+
the spectral range to be included, may be low and high wavelengths in metres or low and high frequencies in
294+
Hertz. Use None for an open bound.
293295
channel : list of int with two elements, optional
294296
the spectral range to be included, the low and high channels (i.e. planes of a cube) inclusive
295297
verbose: bool, optional
@@ -307,8 +309,7 @@ def cutout(self, table, coordinates=None, radius=None, height=None, width=None,
307309

308310
job_url = self._create_job(table, 'cutout_service', verbose)
309311

310-
cutout_spec = self._args_to_payload(coordinates=coordinates, radius=radius, height=height,
311-
width=width, band=band, channel=channel)
312+
cutout_spec = self._args_to_payload(radius=radius, **kwargs)
312313

313314
if not cutout_spec:
314315
raise ValueError("Please provide cutout parameters such as coordinates, band or channel.")
@@ -437,7 +438,6 @@ def _add_cutout_params(self, job_location, verbose, cutout_spec):
437438
log.info("Adding parameters: " + str(cutout_spec))
438439
resp = self._request('POST', job_location + '/parameters', data=cutout_spec, cache=False)
439440
resp.raise_for_status()
440-
return
441441

442442
def _run_job(self, job_location, verbose, poll_interval=20):
443443
"""

docs/casda/casda.rst

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,15 @@ with a username and password. e.g.:
8585
Data Access
8686
===========
8787

88-
In order to access data in CASDA you must first stage the data using the :meth:`~astroquery.casda.CasdaClass.stage_data` method.
88+
In order to access data in CASDA you must first stage the data using the :meth:`~astroquery.casda.CasdaClass.stage_data`
89+
method.
8990
This is because only some of the data in CASDA is held on disc at any particular time.
90-
The :meth:`~astroquery.casda.CasdaClass.stage_data` method should be passed an astropy Table object containing an 'access_url' column.
91+
The :meth:`~astroquery.casda.CasdaClass.stage_data` method should be passed an astropy Table object containing an
92+
'access_url' column.
9193
This column should contain the datalink address of the data product.
9294

93-
Once the data has been assembled you can then download the data using the :meth:`~astroquery.casda.CasdaClass.download_files` method, or using tools such as wget.
95+
Once the data has been assembled you can then download the data using the :meth:`~astroquery.casda.CasdaClass.download_files`
96+
method, or using tools such as wget.
9497
Authentication is required when staging the data, but not for the download.
9598

9699
An example script to download public continuum images of the NGC 7232 region
@@ -117,15 +120,19 @@ Cutouts
117120

118121
As well as accessing full data products, the CASDA service can produce cutout images and cubes from larger data products.
119122
The cutout support in AstroQuery allows both spatial and spectral cutouts.
120-
To produce a spatial cutout, pass in a coordinate and either a radius or a height and a width to the :meth:`~astroquery.casda.CasdaClass.cutout` method.
123+
To produce a spatial cutout, pass in a coordinate and either a radius or a height and a width to the
124+
:meth:`~astroquery.casda.CasdaClass.cutout` method.
121125
To produce a spectral cutout, pass in either a band or a channel value.
122-
For band, the value must be a list or tuple of two `astropy.units.Quantity` objects specifying a low and high frequency or wavelength. For an open ended range use `None` as the open value.
123-
For channel, the value must be a list or tuple of two integers specifying the low and high channels (i.e. planes of a cube) inclusive.
126+
For band, the value must be a list or tuple of two `astropy.units.Quantity` objects specifying a low and high frequency
127+
or wavelength. For an open ended range use `None` as the open value.
128+
For channel, the value must be a list or tuple of two integers specifying the low and high channels (i.e. planes of a
129+
cube) inclusive.
124130
Spatial and spectral parameters can be combined to produce sub-cubes.
125131

126132
Once completed, the cutouts can be downloaded as described in the section above.
127133

128-
An example script to download a cutout from the Rapid ASKAP Continuum Survey (RACS) at a specified position is shown below:
134+
An example script to download a cutout from the Rapid ASKAP Continuum Survey (RACS) at a specified position is shown
135+
below:
129136

130137
.. doctest-skip::
131138

0 commit comments

Comments
 (0)