From 9d41b6823cde1cd7ad171a38523a8f07efb3e885 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Tue, 23 Jan 2018 16:14:25 +0800 Subject: astro/query_ned.py: properly print header; cleanups --- astro/query_ned.py | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/astro/query_ned.py b/astro/query_ned.py index 30791fd..4af11e5 100755 --- a/astro/query_ned.py +++ b/astro/query_ned.py @@ -1,22 +1,12 @@ #!/usr/bin/env python3 # -# Copyright (c) 2016-2017 Weitian LI -# MIT license +# Copyright (c) 2016-2018 Weitian LI +# MIT License # # References: # [1] astroquery: NedClass # https://astroquery.readthedocs.org/en/latest/api/astroquery.ned.NedClass.html # -# Change log: -# 2017-02-11: -# * Add argument "--brief" to not print header -# 2016-05-25: -# * Also output RA, DEC results -# * Update argument process -# * Simplify queried results process -# * Improve comments a bit -# * Some PEP8 fixes -# # TODO: # * allow to query by coordinates & radius range # * filter queried results according to the type/other... @@ -35,15 +25,13 @@ from collections import OrderedDict from astroquery.ned import Ned from astroquery.exceptions import RemoteServiceError -# from astropy import coordinates -# import astropy.units as u # Ned configurations Ned.TIMEOUT = 20 -def query_name(name, verbose=False): +def query_name(name, verbose=False, print_header=False): """ Query NED by source name. """ @@ -58,12 +46,7 @@ def query_name(name, verbose=False): z_flag = q["Redshift Flag"][0].decode("utf-8") refs = q["References"][0] notes = q["Notes"][0] - if verbose: - print("%s: %s,%s,%s,%s,%s,%s,%s,%s,%s" % - (name, objname, objtype, ra, dec, velocity, z, z_flag, - refs, notes), - file=sys.stderr) - except RemoteServiceError as e: + except RemoteServiceError: objname = None objtype = None ra = None @@ -88,6 +71,10 @@ def query_name(name, verbose=False): ("References", refs), ("Notes", notes), ]) + if verbose: + if print_header: + print(",".join(results.keys())) + print(",".join([str(v) for v in results.values()])) return results @@ -102,8 +89,9 @@ def main(): help="be brief and do not print header") parser.add_argument("-i", "--input", dest="input", required=True, help="source names to be queried (sep by comma); " + - "or a file contains the names (one per line)") - parser.add_argument("-o", "--output", dest="output", default=sys.stdout, + "or a file contains the names (one per line)") + parser.add_argument("-o", "--output", dest="output", + default=sys.stdout, help="output CSV file with queried data") args = parser.parse_args() @@ -114,8 +102,11 @@ def main(): results_list = [] + print_header = True for name in names: - qr = query_name(name, verbose=args.verbose) + qr = query_name(name, verbose=args.verbose, + print_header=print_header) + print_header = False results_list.append(qr) try: -- cgit v1.2.2