Skip to content

Commit fd93320

Browse files
authored
Merge pull request #2012 from jwoillez/fix_vizier_list
Many Vizier fixes
2 parents 1a89486 + 570ae48 commit fd93320

File tree

2 files changed

+51
-28
lines changed

2 files changed

+51
-28
lines changed

astroquery/vizier/core.py

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -357,19 +357,20 @@ def query_region_async(self, coordinates, radius=None, inner_radius=None,
357357
catalog = VizierClass._schema_catalog.validate(catalog)
358358
center = {}
359359
columns = []
360+
361+
# Process coordinates
360362
if isinstance(coordinates, (commons.CoordClasses,) + six.string_types):
361363
c = commons.parse_coordinates(coordinates).transform_to('fk5')
362364

363365
if not c.isscalar:
364-
pos_list = []
366+
center["-c"] = []
365367
for pos in c:
366368
ra_deg = pos.ra.to_string(unit="deg", decimal=True,
367369
precision=8)
368370
dec_deg = pos.dec.to_string(unit="deg", decimal=True,
369371
precision=8, alwayssign=True)
370-
pos_list += ["{}{}".format(ra_deg, dec_deg)]
371-
center["-c"] = "<<;" + ";".join(pos_list)
372-
columns += ["_q"] # request a reference to the input table
372+
center["-c"] += ["{}{}".format(ra_deg, dec_deg)]
373+
columns += ["_q"] # Always request reference to input table
373374
else:
374375
ra = c.ra.to_string(unit='deg', decimal=True, precision=8)
375376
dec = c.dec.to_string(unit="deg", decimal=True, precision=8,
@@ -378,7 +379,7 @@ def query_region_async(self, coordinates, radius=None, inner_radius=None,
378379
elif isinstance(coordinates, tbl.Table):
379380
if (("_RAJ2000" in coordinates.keys()) and ("_DEJ2000" in
380381
coordinates.keys())):
381-
pos_list = []
382+
center["-c"] = []
382383
sky_coord = coord.SkyCoord(coordinates["_RAJ2000"],
383384
coordinates["_DEJ2000"],
384385
unit=(coordinates["_RAJ2000"].unit,
@@ -388,15 +389,15 @@ def query_region_async(self, coordinates, radius=None, inner_radius=None,
388389
precision=8)
389390
dec_deg = dec.to_string(unit="deg", decimal=True,
390391
precision=8, alwayssign=True)
391-
pos_list += ["{}{}".format(ra_deg, dec_deg)]
392-
center["-c"] = "<<;" + ";".join(pos_list)
393-
columns += ["_q"] # request a reference to the input table
392+
center["-c"] += ["{}{}".format(ra_deg, dec_deg)]
393+
columns += ["_q"] # Always request reference to input table
394394
else:
395395
raise ValueError("Table must contain '_RAJ2000' and "
396396
"'_DEJ2000' columns!")
397397
else:
398398
raise TypeError("Coordinates must be one of: string, astropy "
399399
"coordinates, or table containing coordinates!")
400+
400401
# decide whether box or radius
401402
if radius is not None:
402403
# is radius a disk or an annulus?
@@ -434,6 +435,7 @@ def query_region_async(self, coordinates, radius=None, inner_radius=None,
434435
raise Exception(
435436
"At least one of radius, width/height must be specified")
436437

438+
# Prepare payload
437439
data_payload = self._args_to_payload(center=center, columns=columns,
438440
catalog=catalog, column_filters=column_filters)
439441

@@ -547,16 +549,13 @@ def _args_to_payload(self, *args, **kwargs):
547549
# keyword names that can mean 'all'
548550
alls = ['all', '**']
549551
if any(x in columns for x in alls):
552+
columns_all = True
550553
for x in alls:
551554
if x in columns:
552555
columns.remove(x)
553-
body['-out.all'] = 2
554-
# keyword name that means default columns
555-
if '*' in columns:
556-
columns.remove('*')
557-
columns_default = True
556+
body['-out.all'] = None
558557
else:
559-
columns_default = False
558+
columns_all = False
560559

561560
# process: columns - identify sorting requests
562561
columns_out = []
@@ -571,16 +570,23 @@ def _args_to_payload(self, *args, **kwargs):
571570
else:
572571
columns_out += [column]
573572

574-
if columns_default:
575-
body['-out'] = '*'
576-
else:
577-
body['-out'] = columns_out
573+
# calculated keyword names that start with an underscore
574+
columns_calc = []
575+
for column in columns_out:
576+
if column[0] == '_':
577+
columns_calc.append(column)
578+
for column in columns_calc:
579+
columns_out.remove(column)
578580

579-
if columns_out:
580-
body['-out.add'] = ','.join(columns_out)
581+
if columns_out and not columns_all:
582+
body['-out'] = ','.join(columns_out)
583+
584+
if columns_calc:
585+
body['-out.add'] = ','.join(columns_calc)
581586

582587
if len(sorts_out) > 0:
583588
body['-sort'] = ','.join(sorts_out)
589+
584590
# process: maximum rows returned
585591
row_limit = kwargs.get('row_limit') or self.ROW_LIMIT
586592
if row_limit < 0:
@@ -607,14 +613,26 @@ def _args_to_payload(self, *args, **kwargs):
607613
if ucd:
608614
body['-ucd'] = ucd
609615

610-
# create final script
611-
script = "\n".join(["{key}={val}".format(key=key, val=val)
612-
for key, val in body.items()])
613-
# add keywords
616+
# create final script starting with keywords
617+
script = []
614618
if (not isinstance(self.keywords, property) and
615619
self.keywords is not None):
616-
script += "\n" + str(self.keywords)
617-
return script
620+
script += [str(self.keywords)]
621+
# add all items that are not lists
622+
for key, val in body.items():
623+
if type(val) is not list:
624+
if val:
625+
script += ["{key}={val}".format(key=key, val=val)]
626+
else:
627+
script += [key]
628+
# add list at the end
629+
for key, val in body.items():
630+
if type(val) is list:
631+
script += ["{key}=<<====AstroqueryList".format(key=key)]
632+
script += val
633+
script += ["====AstroqueryList"]
634+
# merge result
635+
return "\n".join(script)
618636

619637
def _parse_result(self, response, get_catalog_names=False, verbose=False,
620638
invalid='warn'):

astroquery/vizier/tests/test_vizier.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from astropy.table import Table
77
import astropy.units as u
88
import six
9-
from six.moves import urllib_parse as urlparse
109
from ... import vizier
1110
from ...utils import commons
1211
from ...utils.testing_tools import MockResponse
@@ -43,7 +42,13 @@ def post_mockreturn(self, method, url, data=None, timeout=10, files=None,
4342
if isinstance(data, dict):
4443
datad = data
4544
else:
46-
datad = dict([urlparse.parse_qsl(d)[0] for d in data.split('\n')])
45+
datad = {}
46+
for line in data.split('\n'):
47+
if '=' in line:
48+
key, value = line.split('=', maxsplit=1)
49+
datad[key.strip()] = value.strip()
50+
else:
51+
datad[line] = None
4752
if '-source' in datad:
4853
# a request for the actual data
4954
filename = data_path(VO_DATA[datad['-source']])

0 commit comments

Comments
 (0)