Last active
December 14, 2015 23:09
-
-
Save juandesant/5163782 to your computer and use it in GitHub Desktop.
Get RA, Dec from an object name using the CDS Sesame web service in Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib | |
# source is a variable containing a source name, such as M31, Alpha Centauri, Sag A*, etc | |
source = "Sag A*" | |
# This is the string with the URL to query the Sesame service, | |
# using a 'name' parameter. | |
# sesameQueryUrl = 'http://cdsws.u-strasbg.fr/axis/services/Sesame?' +\ | |
# 'method=sesame&resultType=p&all=true&service=NSVA' +\ | |
# '&name=%(name)s' | |
# use the updated CDS REST endpoint | |
sesameQueryUrl = 'http://cdsweb.u-strasbg.fr/cgi-bin/nph-sesame/-op/NSV?%(name)s' | |
# Build the query from the sesameQueryUrl and the dictionary | |
# We have to use urllib.quote to make it safe to include in a URL | |
sesameQuery = sesameQueryUrl % { | |
'name': urllib.quote(source) | |
} | |
# Read the lines of the response from the final URL | |
sesameResponse = urllib.urlopen(sesameQuery).readlines() | |
# Parse the sesameResponse to get RA, Dec | |
# oneliner: ra, dec = filter(lambda x: x.find('%J') == 0, sesameResponse)[0].split(' ')[1:3] | |
# The coordinates are in the lines starting with %J | |
coordinateList = filter(lambda x: x.find('%J') == 0, sesameResponse) | |
# As filter returns a list, get the first line | |
coordinates = coordinateList[0] | |
# Split the coordinates between the spaces, and drop de first item (%J) (so, start from the second on) | |
coordinates = coordinates.split(' ')[1:] | |
ra = coordinates[0] | |
dec = coordinates[1] | |
print ra, dec |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment