""" Cone Search Examples Created 2005-06-01 by Shui Hung Kwok, shkwok at computer.org """ Usage=""" Cone Search Examples: Usage: python ConeSearchEx.py exampleName parameters ... For example: python ConeSearchEx.py coneSearchRaw http://chart.stsci.edu/GSCVO/GSC22VO.jsp 10.689 41.269 0.2 1 python ConeSearchEx.py coneSearchVOTable http://archive.stsci.edu/hst/search.php 53.084 -27.873 0.01 1 exampleName can be: coneSearchRaw URL RA DEC SR VERB URL is URL of the cone search provider service RA right ascension in deg J2000 DEC declination in deg J2000 SR search radius in deg VERB 1=minimal output, 3=all columns Outputs raw result. coneSearchVOTable URL RA DEC SR VERB Same as coneSearchRaw but result in stored in VOTable and then output. printFields URL RA DEC SR VERB Same as coneSearchRaw but outputs the list of fields with names and description, if available. coneSearchCSV URL RA DEC SR VERB Same as coneSearchRaw but outputs the the entire table in comma separated values format. coneSearchList targetList URL Given a file with a targetList. For each target performs a Cone Search and merge the VOTables. Return one VOTable. runAll Runs all examples """ import sys from ConeSearch import ConeSearch from printStruct import printStruct def coneSearchRaw (urn, ra, dec, sr, verb=1): """ ConeSearchRaw: Gets parameters from command line ra = right ascension in deg dec = declination in deg sr = search radious in deg verb = vervosity [1,2,3] Instantiates ConeSearch object Calls getRaw() method """ conSearch = ConeSearch (urn) print conSearch.getRaw (ra, dec, sr, verb) def coneSearchVOTable (urn, ra, dec, sr, verb=1): """ ConeSearchVOTable Similar to coneSearchRaw, but calls getVOTable() method instead. getVOTable () returns a VOTable. Calls walk() method to output votable on standard output """ conSearch = ConeSearch (urn) conResult = conSearch.getVOTable (ra, dec, sr, verb) conResult.printAllNodes () def helperWhatName (attrs): """ Given a dict of attributes. Finds if there is a name or id. Returns that key, in any lower/upper case spelling. """ keys = attrs.keys () for n in ['name', 'id', 'ID', 'NAME']: if n in keys: return n return 'ID' def printFields (urn, ra, dec, sr, verb=1): """ PrintFields Similar to coneSearchRaw, but outputs name and description of fields. Descriptions may not be available. Names of fields can be stored as attributes name, id, or ID. """ conSearch = ConeSearch (urn) conResult = conSearch.getVOTable (ra, dec, sr, verb) fields = conResult.getFields () # list of VONodes for i,fd in enumerate (fields): attr = fd.getAttributes () name = attr.get (helperWhatName (attr)) ucd = attr.get ('ucd') desc = fd.getNode ('/DESCRIPTION') descStr = '' if desc != None: descStr = ', ' + desc print "Field[%i]= %s,%s,%s" % (i, name, ucd, descStr) def printVOT2CSV (vot): def mywrite (str): print str vot.output2CSV (mywrite) def coneSearchCSV (urn, ra, dec, sr, verb=1): """ ConeSearchCSV Similar to coneSearchRaw, but outputs content in CSV format. """ conSearch = ConeSearch (urn) conResult = conSearch.getVOTable (ra, dec, sr, verb) printVOT2CSV (conResult) def coneSearchList (listName, srvURN): """ Par1: Name of input cvs file containing a list of target positions (Name,RA,DEC,SR) Par2: service URN of a cone search service Reads cvs file and for each target in the list perform a cone search. Merge all results together and outputs one VOTable. """ fh = file (listName) conSearch = ConeSearch (srvURN) res = None for line in fh: line = line.strip () if not line: continue if line.startswith ('#'): continue name, ra, dec, sr = line.strip().split (',') print "querying %s %s %s" % (ra, dec, sr) conResult = conSearch.getVOTable (ra, dec, sr) if not res: res = conResult else: print "appending" res.append (conResult) print "formatting" printVOT2CSV (res) def runAll (): """ Tests all examples """ print "\nCone Search Raw:\n%s" % ('*' * 40) coneSearchRaw ('http://archive.stsci.edu/hst/search.php', 53.084, -27.873, 0.01, 1) print "\nCone Search VOTable:\n%s" % ('*' * 40) coneSearchVOTable ('http://archive.stsci.edu/hst/search.php', 53.084, -27.873, 0.01, 1) print "\nPrintFields:\n%s" % ('*' * 40) printFields ('http://archive.stsci.edu/hst/search.php', 53.084, -27.873, 0.01, 1) print "\nCone Search CSV:\n%s" % ('*' * 40) coneSearchCSV ('http://archive.stsci.edu/hst/search.php', 53.084, -27.873, 0.01, 1) print "\nCone Search List:\n%s" % ('*' * 40) coneSearchList ('../data/targets.csv', 'http://archive.stsci.edu/hst/search.php') if __name__ == "__main__": try: vars ()[sys.argv[1]](*sys.argv[2:]) except Exception, e: # Uncomment these for debugging import traceback traceback.print_exc () print "Failed to execute example ", e print Usage