Skip to content

Commit 4c6b924

Browse files
committed
Refactor region boundary determination in vizier
The `query_region()` and `query_region_async()` methods in `vizier` allow specifying the inner and outer radii of an annulus, or the width and height of a rectangle. The code that ensures these values are interpreted correctly even if their units are different is now shorter.
1 parent 24c6b9e commit 4c6b924

File tree

2 files changed

+23
-48
lines changed

2 files changed

+23
-48
lines changed

astroquery/vizier/core.py

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -426,39 +426,18 @@ def query_region_async(self, coordinates, radius=None, inner_radius=None,
426426
raise TypeError("Coordinates must be one of: string, astropy "
427427
"coordinates, or table containing coordinates!")
428428

429-
# decide whether box or radius
430429
if radius is not None:
431-
# is radius a disk or an annulus?
432430
if inner_radius is None:
433-
radius = coord.Angle(radius)
434-
unit, value = _parse_angle(radius)
435-
key = "-c.r" + unit
436-
center[key] = value
431+
_, unit_str, o_radius = _parse_angle(radius)
432+
center["-c.r" + unit_str] = str(o_radius)
437433
else:
438-
i_radius = coord.Angle(inner_radius)
439-
o_radius = coord.Angle(radius)
440-
if i_radius.unit != o_radius.unit:
441-
o_radius = o_radius.to(i_radius.unit)
442-
i_unit, i_value = _parse_angle(i_radius)
443-
o_unit, o_value = _parse_angle(o_radius)
444-
key = "-c.r" + i_unit
445-
center[key] = ",".join([str(i_value), str(o_value)])
434+
unit, unit_str, i_radius = _parse_angle(inner_radius)
435+
o_radius = coord.Angle(radius).to_value(unit)
436+
center["-c.r" + unit_str] = f"{i_radius},{o_radius}"
446437
elif width is not None:
447-
# is box a rectangle or square?
448-
if height is None:
449-
width = coord.Angle(width)
450-
unit, value = _parse_angle(width)
451-
key = "-c.b" + unit
452-
center[key] = "x".join([str(value)] * 2)
453-
else:
454-
w_box = coord.Angle(width)
455-
h_box = coord.Angle(height)
456-
if w_box.unit != h_box.unit:
457-
h_box = h_box.to(w_box.unit)
458-
w_unit, w_value = _parse_angle(w_box)
459-
h_unit, h_value = _parse_angle(h_box)
460-
key = "-c.b" + w_unit
461-
center[key] = "x".join([str(w_value), str(h_value)])
438+
unit, unit_str, w_box = _parse_angle(width)
439+
h_box = w_box if height is None else coord.Angle(height).to_value(unit)
440+
center["-c.b" + unit_str] = f"{w_box}x{h_box}"
462441
else:
463442
raise Exception(
464443
"At least one of radius, width/height must be specified")
@@ -806,17 +785,15 @@ def _parse_angle(angle):
806785
807786
Returns
808787
-------
809-
(unit, value) : tuple
810-
formatted for Vizier.
788+
(unit, unit_str, value) : tuple
811789
"""
812790
angle = coord.Angle(angle)
813-
if angle.unit == u.arcsec:
814-
unit, value = 's', angle.value
815-
elif angle.unit == u.arcmin:
816-
unit, value = 'm', angle.value
791+
if angle.unit is u.arcsec:
792+
return u.arcsec, "s", angle.value
793+
elif angle.unit is u.arcmin:
794+
return u.arcmin, "m", angle.value
817795
else:
818-
unit, value = 'd', angle.to(u.deg).value
819-
return unit, value
796+
return u.deg, "d", angle.to_value(u.deg)
820797

821798

822799
class VizierKeyword(list):

astroquery/vizier/tests/test_vizier.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,16 @@ def patch_coords(request):
7272
return mp
7373

7474

75-
@pytest.mark.parametrize(('dim', 'expected_out'),
76-
[(5 * u.deg, ('d', 5)),
77-
(5 * u.arcmin, ('m', 5)),
78-
(5 * u.arcsec, ('s', 5)),
79-
(0.314 * u.rad, ('d', 18)),
80-
('5d5m5.5s', ('d', 5.0846))
81-
])
82-
def test_parse_angle(dim, expected_out):
83-
actual_out = vizier.core._parse_angle(dim)
84-
actual_unit, actual_value = actual_out
85-
expected_unit, expected_value = expected_out
75+
@pytest.mark.parametrize("dim,expected_unit,expected_unit_str,expected_value",
76+
[(5 * u.deg, u.deg, "d", 5),
77+
(5 * u.arcmin, u.arcmin, "m", 5),
78+
(5 * u.arcsec, u.arcsec, "s", 5),
79+
(0.314 * u.rad, u.deg, "d", 18),
80+
("5d5m5.5s", u.deg, "d", 5.0846)])
81+
def test_parse_angle(dim, expected_unit, expected_unit_str, expected_value):
82+
actual_unit, actual_unit_str, actual_value = vizier.core._parse_angle(dim)
8683
assert actual_unit == expected_unit
84+
assert actual_unit_str == expected_unit_str
8785
npt.assert_approx_equal(actual_value, expected_value, significant=2)
8886

8987

0 commit comments

Comments
 (0)