Skip to content

Commit d635433

Browse files
committed
Add a spectral cutout example
Also include support for numpy arrays for band and channel, and autocorrect the band and channel values to be in ascending order
1 parent c69651f commit d635433

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

astroquery/casda/core.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from astropy.table import Table
1515
from astropy.io.votable import parse
1616
from astroquery import log
17+
import numpy as np
1718

1819
# 3. local imports - use relative imports
1920
# commonly required local imports shown below as example
@@ -125,7 +126,7 @@ def _args_to_payload(self, radius=1*u.arcmin, **kwargs):
125126
if channel is not None:
126127
raise ValueError("Either 'channel' or 'band' values may be provided but not both.")
127128

128-
if (not isinstance(band, (list, tuple))) or len(band) != 2 or \
129+
if (not isinstance(band, (list, tuple, np.ndarray))) or len(band) != 2 or \
129130
(band[0] is not None and not isinstance(band[0], u.Quantity)) or \
130131
(band[1] is not None and not isinstance(band[1], u.Quantity)):
131132
raise ValueError("The 'band' value must be a list of 2 wavelength or frequency values.")
@@ -137,22 +138,31 @@ def _args_to_payload(self, radius=1*u.arcmin, **kwargs):
137138
if bandBoundedLow or bandBoundedHigh:
138139
unit = band[0].unit if bandBoundedLow else band[1].unit
139140
if unit.physical_type == 'length':
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)
141+
min_band = '-Inf' if not bandBoundedLow else band[0].to(u.m).value
142+
max_band = '+Inf' if not bandBoundedHigh else band[1].to(u.m).value
142143
elif unit.physical_type == 'frequency':
143144
# Swap the order when changing frequency to wavelength
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)
145+
min_band = '-Inf' if not bandBoundedHigh else band[1].to(u.m, equivalencies=u.spectral()).value
146+
max_band = '+Inf' if not bandBoundedLow else band[0].to(u.m, equivalencies=u.spectral()).value
146147
else:
147148
raise ValueError("The 'band' values must be wavelengths or frequencies.")
149+
# If values were provided in the wrong order, swap them
150+
if bandBoundedLow and bandBoundedHigh and min_band > max_band:
151+
temp_val = min_band
152+
min_band = max_band
153+
max_band = temp_val
148154

149155
request_payload['BAND'] = f'{min_band} {max_band}'
150156

151157
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):
158+
if not isinstance(channel, (list, tuple, np.ndarray)) or len(channel) != 2 or \
159+
not isinstance(channel[0], (int, np.integer)) or not isinstance(channel[1], (int, np.integer)):
154160
raise ValueError("The 'channel' value must be a list of 2 integer values.")
155-
request_payload['CHANNEL'] = f'{channel[0]} {channel[1]}'
161+
if channel[0] <= channel[1]:
162+
request_payload['CHANNEL'] = f'{channel[0]} {channel[1]}'
163+
else:
164+
# If values were provided in the wrong order, swap them
165+
request_payload['CHANNEL'] = f'{channel[1]} {channel[0]}'
156166

157167
return request_payload
158168

@@ -310,7 +320,8 @@ def cutout(self, table, *, coordinates=None, radius=None, height=None,
310320

311321
job_url = self._create_job(table, 'cutout_service', verbose)
312322

313-
cutout_spec = self._args_to_payload(radius=radius, **kwargs)
323+
cutout_spec = self._args_to_payload(radius=radius, coordinates=coordinates, height=height, width=width,
324+
band=band, channel=channel, verbose=verbose)
314325

315326
if not cutout_spec:
316327
raise ValueError("Please provide cutout parameters such as coordinates, band or channel.")

astroquery/casda/tests/test_casda.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from astropy.table import Table, Column
1313
from astropy.io.votable import parse
1414
from astroquery import log
15+
import numpy as np
1516

1617
from astroquery.casda import Casda
1718

@@ -348,6 +349,10 @@ def test_args_to_payload_band():
348349
assert payload['BAND'] == '0.195 0.215'
349350
assert list(payload.keys()) == ['BAND']
350351

352+
payload = casda._args_to_payload(band=(0.215*u.m, 0.195*u.m))
353+
assert payload['BAND'] == '0.195 0.215'
354+
assert list(payload.keys()) == ['BAND']
355+
351356
payload = casda._args_to_payload(band=(0.195*u.m, 21.5*u.cm))
352357
assert payload['BAND'] == '0.195 0.215'
353358
assert list(payload.keys()) == ['BAND']
@@ -364,6 +369,10 @@ def test_args_to_payload_band():
364369
assert payload['BAND'] == '0.19986163866666667 0.21112144929577467'
365370
assert list(payload.keys()) == ['BAND']
366371

372+
payload = casda._args_to_payload(band=np.array([1.5, 1.42])*u.GHz)
373+
assert payload['BAND'] == '0.19986163866666667 0.21112144929577467'
374+
assert list(payload.keys()) == ['BAND']
375+
367376
payload = casda._args_to_payload(band=(None, 1.5*u.GHz))
368377
assert payload['BAND'] == '0.19986163866666667 +Inf'
369378
assert list(payload.keys()) == ['BAND']
@@ -393,7 +402,7 @@ def test_args_to_payload_band_invalid():
393402
assert "The 'band' values must have the same kind of units." in str(excinfo.value)
394403

395404
with pytest.raises(ValueError) as excinfo:
396-
casda._args_to_payload(band=(1.42*u.radian, 21*u.deg))
405+
casda._args_to_payload(band=[1.42*u.radian, 21*u.deg])
397406
assert "The 'band' values must be wavelengths or frequencies." in str(excinfo.value)
398407

399408
with pytest.raises(ValueError) as excinfo:
@@ -407,7 +416,11 @@ def test_args_to_payload_channel():
407416
assert payload['CHANNEL'] == '0 30'
408417
assert list(payload.keys()) == ['CHANNEL']
409418

410-
payload = casda._args_to_payload(channel=(17, 23))
419+
payload = casda._args_to_payload(channel=np.array([17, 23]))
420+
assert payload['CHANNEL'] == '17 23'
421+
assert list(payload.keys()) == ['CHANNEL']
422+
423+
payload = casda._args_to_payload(channel=(23, 17))
411424
assert payload['CHANNEL'] == '17 23'
412425
assert list(payload.keys()) == ['CHANNEL']
413426

docs/casda/casda.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Spatial and spectral parameters can be combined to produce sub-cubes.
131131

132132
Once completed, the cutouts can be downloaded as described in the section above.
133133

134-
An example script to download a cutout from the Rapid ASKAP Continuum Survey (RACS) at a specified position is shown
134+
An example script to download a 2D cutout from the Rapid ASKAP Continuum Survey (RACS) at a specified position is shown
135135
below:
136136

137137
.. doctest-skip::
@@ -152,6 +152,26 @@ below:
152152
>>> url_list = casda.cutout(subset[:1], coordinates=centre, radius=14*u.arcmin)
153153
>>> filelist = casda.download_files(url_list, savedir='/tmp')
154154

155+
An example script to download a 3D cutout from the WALLABY Pre-Pilot Eridanus cube at a specified position and velocity
156+
is shown below:
157+
158+
.. doctest-skip::
159+
160+
>>> from astropy import coordinates, units as u, wcs
161+
>>> from astroquery.casda import Casda
162+
>>> import getpass
163+
>>> centre = coordinates.SkyCoord.from_name('NGC 1371')
164+
>>> username = '[email protected]'
165+
>>> password = getpass.getpass(str("Enter your OPAL password: "))
166+
>>> casda = Casda(username, password)
167+
>>> result = Casda.query_region(centre, radius=30*u.arcmin)
168+
>>> public_data = Casda.filter_out_unreleased(result)
169+
>>> eridanus_cube = public_data[public_data['filename'] == 'Eridanus_full_image_V3.fits']
170+
>>> vel = np.array([1250, 1600])*u.km/u.s
171+
>>> freq = vel.to(u.Hz, equivalencies=u.doppler_radio(1.420405751786*u.GHz))
172+
>>> url_list = casda.cutout(eridanus_cube, coordinates=centre, radius=9*u.arcmin, band=freq)
173+
>>> filelist = casda.download_files(url_list, savedir='/tmp')
174+
155175

156176
Reference/API
157177
=============

0 commit comments

Comments
 (0)