# This code goes with the blog post
# https://blog.g-vo.org/lamost5-meets-datalink/
# Put into the public domain by Markus Demleitner
#
import io
import warnings
warnings.filterwarnings('ignore', module="astropy.io.votable.*")
from astropy import units as u
from astropy.io import votable
from scipy.optimize import curve_fit
import numpy
import pyvo
def get_param(vot, utype):
"""returns the value of a param with utype in the votable vot.
If no such param exists, return None.
"""
for param in vot.params:
if param.utype and param.utype.lower()==utype:
break
else:
return None
return param.value
def get_profiles(spec_pat, band, percent_sample, min_snr):
"""yields pairs of full accref, spec_table for LAMOST5 cutout
spectra.
spec_pat is a SQL pattern for the spectral type, band an astropy
interval for the spectral region you're interested in, percent_sample
a float between 0 and 100 giving what percent of the table you'd
like to search, and min_snr a float giving a minimum on the SNR
in the region you're cutting out -- spectra below that won't be
returned.
The spectra a record arrays with spectral, flux, flux_error, and flags
columns as defined by GAVO's LAMOST service. The spectral column
is corrected for redshift as given by LAMOST (i.e., they're more or
less in the star's rest frame).
"""
svc = pyvo.dal.TAPService("http://dc.g-vo.org/tap")
for dl in svc.run_sync("select ssa_pubdid, ssa_targsubclass,"
" ssa_redshift, accref"
" from lamost5.data tablesample({})"
" where ssa_targsubclass like '{}'".format(
percent_sample, spec_pat)).iter_datalinks():
proc = next(dl.iter_procs())
try:
cutout = proc.processed(band=band)
except pyvo.DALServiceError:
# something is wrong this spectrum. For our purposes, ignore it.
continue
vot = votable.parse(io.BytesIO(cutout.read())).get_first_table()
points = vot.to_table()
if sum(points["flux"])/sum(points["flux_error"])1:
break
print()
print(accref.decode("ascii"))