""" SIAP Examples Created 2005-06-03 by Shui Hung Kwok, shkwok at computer.org """ Usage=""" SIAP Examples: Usage: python SIAPEx.py exampleName parameters ... For example: python SIAPEx.py siapRaw 'http://www.cadc-ccda.hia-iha.nrc-cnrc.gc.ca/ivoa/CFHT/siapQuery?' 10.689 41.269 0.2 python SIAPEx.py siapVOTable 'http://casjobs.sdss.org/vo/DR3SIAP/SIAP.asmx/getSiapInfo?BANDPASS=u&' 180 0 0.2 0 ALL python SIAPEx.py printFields 'http://skyserver.sdss.org/vo/DR1SIAP/SIAP.aspx?bandpass=*' 180 0 0.2 0 python SIAPEx.py siapCSV 'http://skyserver.sdss.org/vo/DR1SIAP/SIAP.aspx?bandpass=*' 180 0 0.2 0 python SIAPEx.py siapImages 'http://skyserver.sdss.org/vo/DR1SIAP/SIAP.aspx?bandpass=*' 180 0 0.2 0 python SIAPEx.py runAll exampleName can be: siapRaw urn ra dec size verb Performs a SIAP query urn service URL ra right ascension in decimal degree dec declination in decimal degree size in decimal degree verb = [0,3] Outputs the raw result. siapVOTable urn ra dec size verb format Similar to siapRaw. format: ALL, GRAPHIC, METADATA, image/fits, image.jpeg Stores result as VOTable and prints result. siapVOTableInfo urn ra dec size verb format Similar to siapVOTable. Search for INFO tag to get query status printFields urn ra dec size verb Similar to siapVOTable. Prints list of fields of VOTable siapCSV urn ra dec size verb Similar to siapVOTable. Prints content in CSV format. siapImages urn ra dec size verb Performs query and retrieves images. Hardwired to get image/jpeg. To avoid filling up the disk only 5 images are saved. runAll Runs all examples """ import sys from SIAP import SIAP, retrieveImage from ConeSearchEx import helperWhatName , printVOT2CSV from urllib import urlopen def siapRaw (urn, ra, dec, size, verb=0): """ Performs a query and print raw result. """ siap = SIAP (urn) print siap.getRaw (ra, dec, size, verb, FORMAT='ALL') def siapVOTable (urn, ra, dec, size, verb=0, format='ALL'): """ Simlar to siapRaw Performs a query, save result in VOTable and outputs content. """ siap = SIAP (urn) result = siap.getVOTable (ra, dec, size, verb, FORMAT=format) result.printAllNodes () def siapVOTableInfo (urn, ra, dec, size, verb=0, format='ALL'): """ Simlar to siapVOTable. Looks for Info tag. """ siap = SIAP (urn) result = siap.getVOTable (ra, dec, size, verb, FORMAT=format) try: result.printAllNodes () print '*' * 40 info = result.getNode ('/VOTABLE/RESOURCE/INFO') # also try '/VOTABLE/INFO' attrs = info.getAttributes () attrVal = attrs['value'] print "Info: %s\n%s" % (attrVal, info.getContent ()) except Exception, e: print "Failed to get info: %s" % e def printFields (urn, ra, dec, size, verb=0): """ Similar to siapVOTable. Performs a query and prints the fields. """ siap = SIAP (urn) result = siap.getVOTable (ra, dec, size, verb, FORMAT='METADATA') fields = result.getFields () # list of VONodes if fields == None: raise Exception, 'nothing found' 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 siapCSV (urn, ra, dec, size, verb=0): """ Performs a query and outputs table in CSV. """ siap = SIAP (urn) result = siap.getVOTable (ra, dec, size, verb, FORMAT='ALL') printVOT2CSV (result) def siapImages (urn, ra, dec, size, verb=0, format='image/jpeg', nrdownload=5): """ Performs a query and retrieves the first 5 images. Requests jpeg images only. """ siap = SIAP (urn) result = siap.getVOTable (ra, dec, size, verb, FORMAT=format) # Get column index for access url urlColIdx = result.getColumnIdx ('VOX:Image_AccessReference') formatIdx = result.getColumnIdx ('VOX:Image_Format') if urlColIdx < 0: print "No access reference found" return dataRows = result.getDataRows () getData = result.getData print "Number of records: ", len (dataRows) cnt = 0 for row in dataRows: cells = getData (row) url = cells[urlColIdx] fmt = cells[formatIdx] ext = '.' if 'jpeg' in fmt: ext += 'jpg' elif 'fits' in fmt: ext += 'fits' elif 'gif' in fmt: ext += 'gif' else: ext = '' iname = "image%d%s" % (cnt, ext) print "Saving image %d as %s" % (cnt, iname) retrieveImage (url, iname) cnt += 1 if cnt > nrdownload: break def runAll (): """ Tests all examples """ print "\nSIAP Raw:\n%s" % ('*' * 40) siapRaw ('http://skyserver.sdss.org/vo/DR1SIAP/SIAP.aspx?bandpass=*', 180, 0, 0.2, 1) print "\nSIAP VOTable:\n%s" % ('*' * 40) siapVOTable ('http://skyserver.sdss.org/vo/DR1SIAP/SIAP.aspx?bandpass=*', 180, 0, 0.2, 2) print "\nSIAP VOTable Info:\n%s" % ('*' * 40) siapVOTableInfo ('http://skyserver.sdss.org/vo/DR1SIAP/SIAP.aspx?bandpass=*', 180, 0, 0.2, 2) print "\nPrint Fields:\n%s" % ('*' * 40) printFields ('http://skyserver.sdss.org/vo/DR1SIAP/SIAP.aspx?bandpass=*', 180, 0, 0.2, 3) print "\nSIAP CSV:\n%s" % ('*' * 40) siapCSV ('http://skyserver.sdss.org/vo/DR1SIAP/SIAP.aspx?bandpass=*', 180, 0, 0.2, 0) print "\nSIAP Images:\n%s" % ('*' * 40) siapImages ('http://skyserver.sdss.org/vo/DR1SIAP/SIAP.aspx?bandpass=*', 180, 0, 0.2, 1) 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