SSA: Retrieving and Parsing a VOTable
If you want to quickly generate some download links from the SSA service, the following may be suitable:
- Create a string containing your query request.
- Send the request and download the VOTABLE results.
- Parse the VOTABLE file and extract the download links to each spectrum.
- Download the spectra internally or by printing out wget commands.
An example Python script is provided below. It requires the astropy module to be installed.
from astropy.io.votable import parse_single_table import requests from io import BytesIO from astropy.time import Time #SSA query URL #query 6dF final data release only around RA=55 deg, DEC=-29.1 deg in a circle of 0.4 deg diameter url = "https://datacentral.org.au/vo/ssa/query?POS=55,-29.1&SIZE=0.4&REQUEST=queryData&COLLECTION=6dfgs_fdr" #perform the query via the above url and store contents in vot vot = requests.get(url).content #optionally write table to file #fptr = open("ssa_table.xml","wb") #fptr.write(vot) #fptr.close() #open the table with astropy's votable package table = parse_single_table(BytesIO(vot)) data = table.array #Go through each row in the table for i in range (0,len(data)): #For 6dF spectra the band_name is either V, R or VR (the combined V+R spectrum) band_name = data['band_name'][i] #Since multiple observations may be available, use the midpoint of the observation #in the filename to ensure all unique spectra are retrieved midpoint = data['t_midpoint'][i] #optionally, convert midpoint (MJD) to date string using astropy #trimming off any fractional seconds at the end with '[:-4]' t = Time(midpoint,format='mjd') t.format='isot' midpoint = t.value[:-4] target_name = data['target_name'][i] #URL of the spectrum; N.B. We add RESPONSEFORMAT=fits to get fits format #otherwise the VO table is returned by default which would then have to be parsed #to get the full link to the fits file access_url = data['access_url'][i] + "&RESPONSEFORMAT=fits" #filename to save spectrum to ofname = "%s-%s-%s.fits" % (target_name,band_name,midpoint) #Print out a wget command to download the data #download_cmd = "wget -O %s \"%s\"" % (ofname,access_url) #print (download_cmd) #alternatively, download directly... print ("Downloading ",ofname) spec = requests.get(access_url).content fptr = open(ofname,"wb") fptr.write(spec) fptr.close()