Skip to content

Commit 93267cb

Browse files
authored
Merge pull request #2810 from e-koch/fix_alma_footprint_to_reg
Fix for alma.utils.footprint_to_reg
2 parents 6267912 + 730c579 commit 93267cb

File tree

4 files changed

+88
-5
lines changed

4 files changed

+88
-5
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ alma
7676
- New DataLink API handling. [#2493]
7777
- Fixed bug #2489 in which blank URLs were being sent to the downloader [#2490]
7878
- Removed deprecated broken functions from ``alma.utils``. [#2331]
79+
- Fixed a bug in slicing of ALMA regions. [#2810]
7980

8081
astrometry_net
8182
^^^^^^^^^^^^^^

astroquery/alma/tests/test_alma_utils.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2+
import pytest
3+
24
import numpy as np
35
from astropy import units as u
6+
from astropy.coordinates import SkyCoord
7+
8+
try:
9+
from regions import CircleSkyRegion
10+
11+
HAS_REGIONS = True
12+
except ImportError:
13+
HAS_REGIONS = False
414

515
from .. import utils
616

@@ -22,3 +32,68 @@ def test_parse_frequency_support(frq_sup_str=frq_sup_str, result=franges):
2232
def approximate_primary_beam_sizes(frq_sup_str=frq_sup_str,
2333
beamsizes=beamsizes):
2434
assert np.all(utils.approximate_primary_beam_sizes(frq_sup_str) == beamsizes)
35+
36+
37+
mosaic_footprint_str = '''Polygon ICRS 266.519781 -28.724666 266.524678 -28.731930 266.536683
38+
-28.737784 266.543860 -28.737586 266.549277 -28.733370 266.558133
39+
-28.729545 266.560136 -28.724666 266.558845 -28.719605 266.560133
40+
-28.694332 266.555234 -28.687069 266.543232 -28.681216 266.536058
41+
-28.681414 266.530644 -28.685630 266.521788 -28.689453 266.519784
42+
-28.694332 266.521332 -28.699778'''
43+
44+
45+
@pytest.mark.skipif(not HAS_REGIONS, reason="regions is required")
46+
def test_footprint_to_reg_mosaic(mosaic_footprint_str=mosaic_footprint_str):
47+
48+
pairs = zip(mosaic_footprint_str.split()[2::2], mosaic_footprint_str.split()[3::2])
49+
vertices = [SkyCoord(float(ra) * u.deg, float(dec) * u.deg, frame='icrs')
50+
for ra, dec in pairs]
51+
52+
reg_output = utils.footprint_to_reg(mosaic_footprint_str)
53+
54+
assert len(reg_output) == 1
55+
56+
for vertex in vertices:
57+
assert vertex in reg_output[0].vertices
58+
59+
60+
@pytest.mark.skipif(not HAS_REGIONS, reason="regions is required")
61+
def test_footprint_to_reg_mosaic_multiple(mosaic_footprint_str=mosaic_footprint_str):
62+
63+
multiple_mosaic_footprint_str = f"{mosaic_footprint_str} {mosaic_footprint_str}"
64+
65+
print(multiple_mosaic_footprint_str)
66+
67+
pairs = zip(mosaic_footprint_str.split()[2::2], mosaic_footprint_str.split()[3::2])
68+
vertices = [SkyCoord(float(ra) * u.deg, float(dec) * u.deg, frame='icrs')
69+
for ra, dec in pairs]
70+
71+
reg_output = utils.footprint_to_reg(multiple_mosaic_footprint_str)
72+
73+
assert len(reg_output) == 2
74+
75+
for this_region in reg_output:
76+
77+
for vertex in vertices:
78+
assert vertex in this_region.vertices
79+
80+
81+
pointing_footprint_str = 'Circle ICRS 266.519781 -28.724666 0.01'
82+
83+
84+
@pytest.mark.skipif(not HAS_REGIONS, reason="regions is required")
85+
def test_footprint_to_reg_pointing(pointing_footprint_str=pointing_footprint_str):
86+
87+
ra, dec, rad = pointing_footprint_str.split()[2:]
88+
89+
center = SkyCoord(float(ra) * u.deg, float(dec) * u.deg, frame='icrs')
90+
91+
actual_reg = CircleSkyRegion(center, radius=float(rad) * u.deg)
92+
93+
reg_output = utils.footprint_to_reg(pointing_footprint_str)
94+
95+
assert len(reg_output) == 1
96+
97+
assert actual_reg.center.ra == reg_output[0].center.ra
98+
assert actual_reg.center.dec == reg_output[0].center.dec
99+
assert actual_reg.radius == reg_output[0].radius

astroquery/alma/utils.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ def footprint_to_reg(footprint):
5050
if footprint[:7] != 'Polygon' and footprint[:6] != 'Circle':
5151
raise ValueError("Unrecognized footprint type")
5252

53-
import regions
53+
try:
54+
import regions
55+
except ImportError:
56+
print('Could not import `regions`, which is required for the '
57+
'functionality of this function.')
58+
raise
5459

5560
reglist = []
5661

@@ -61,17 +66,19 @@ def footprint_to_reg(footprint):
6166
entries = footprint.split()
6267
if entries[0] == 'Circle':
6368
center = SkyCoord(float(entries[2]), float(entries[3]), frame='icrs', unit=(u.deg, u.deg))
64-
reg = regions.CircleSkyRegion(center, radius=float(entries[4]), meta=meta, visual=visual)
69+
reg = regions.CircleSkyRegion(center, radius=float(entries[4])*u.deg,
70+
meta=meta, visual=visual)
6571
reglist.append(reg)
6672

6773
else:
6874
polygons = [index for index, entry in enumerate(entries) if entry == 'Polygon']
6975

70-
for start, stop in zip(polygons, polygons[1:]+[None]):
76+
for start, stop in zip(polygons, polygons[1:]+[len(entries)]):
77+
start += 1
7178
ra = [float(x) for x in entries[start+1:stop:2]]*u.deg
7279
dec = [float(x) for x in entries[start+2:stop:2]]*u.deg
7380
vertices = SkyCoord(ra, dec, frame='icrs')
74-
reg = regions.PolygonSkyCoord(vertices=vertices, meta=meta, visual=visual)
81+
reg = regions.PolygonSkyRegion(vertices=vertices, meta=meta, visual=visual)
7582
reglist.append(reg)
7683

7784
return reglist

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ and for running the tests:
114114
* `pytest-rerunfailures <https://github.com/pytest-dev/pytest-rerunfailures>`__
115115

116116
The following packages are optional dependencies and are required for the
117-
full functionality of the `~astroquery.mocserver` module:
117+
full functionality of the `~astroquery.mocserver`, `~astroquery.alma`, and `~astroquery.xmatch` modules:
118118

119119
* `astropy-healpix <http://astropy-healpix.readthedocs.io/en/latest/>`_
120120
* `regions <https://astropy-regions.readthedocs.io/en/latest/>`_

0 commit comments

Comments
 (0)