Source code for threeML.catalogs.VirtualObservatoryCatalog

import logging

import warnings

import astropy
import astropy.table as astro_table
import astropy.units as u
from astropy.coordinates import SkyCoord
from astropy.coordinates.name_resolve import get_icrs_coordinates
from astroquery.vo_conesearch import conesearch
from astroquery.vo_conesearch.exceptions import VOSError

# from astropy.vo.client.vos_catalog import VOSCatalog
from astroquery.vo_conesearch.vos_catalog import VOSCatalog


from threeML.io.network import internet_connection_is_active

log = logging.getLogger(__name__)


[docs] class ConeSearchFailed(RuntimeError): pass
[docs] class VirtualObservatoryCatalog(object): def __init__(self, name, url, description): self.catalog = VOSCatalog.create(name, url, description=description) self._get_vo_table_from_source() self._last_query_results = None
[docs] def search_around_source(self, source=None, radius=None, **kwargs): """Search for sources around the named source. The coordinates of the provided source are resolved using the astropy.coordinates.name_resolve facility. :param source: name of the source, like "Crab" or an astropy SkyCoord :param radius: radius of the search, in degrees :return: (ra, dec, table), where ra,dec are the coordinates of the source as resolved by astropy, and table is a table with the list of sources """ # support renamed keyword if kwargs.get("source_name", None) is not None: source = get_icrs_coordinates(kwargs.get("source_name")) warnings.warn( "The 'source_name' keyword in search_around_source is deprecated, use" " 'source' instead", DeprecationWarning, ) if not isinstance(source, astropy.coordinates.SkyCoord): sky_coord = get_icrs_coordinates(source) else: sky_coord = source if radius is None: raise ValueError("You need to provide a radius") ra, dec = (sky_coord.fk5.ra.value, sky_coord.fk5.dec.value) return ra, dec, self.cone_search(ra, dec, radius)
@property def ra_center(self): return self._ra @property def dec_center(self): return self._dec
[docs] def apply_format(self, table): raise NotImplementedError("You have to override this!")
[docs] def get_model(self): raise NotImplementedError("You have to override this!")
def _get_vo_table_from_source(self): raise NotImplementedError("You have to override this!")
[docs] def query(self, query): """ query the entire VO table for the given logical argument. Queries are in the form of pandas queries: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.quer y.html To obtain a preview of the availble columns, try catalog.variables :param query: pandas style query string :return: """ assert type(query) is str, "query must be a string" query_results = self._vo_dataframe.query(query) table = astro_table.Table.from_pandas(query_results) name_column = astro_table.Column(name="name", data=query_results.index) table.add_column(name_column, index=0) out = self.apply_format(table) self._last_query_results = query_results return out
[docs] def query_sources(self, *sources): """Query for the specific source names. :param sources: source(s) to search for :return: """ valid_sources = [] for source in sources: if self._source_is_valid(source): valid_sources.append(source) if valid_sources: query_string = " | ".join(['(index == "%s")' % x for x in valid_sources]) query_results = self._vo_dataframe.query(query_string) table = astro_table.Table.from_pandas(query_results) name_column = astro_table.Column(name="name", data=query_results.index) table.add_column(name_column, index=0) out = self.apply_format(table) self._last_query_results = query_results return out else: RuntimeError("There were not valid sources in your search")
def _source_is_valid(self, source): raise NotImplementedError("You have to override this!") @property def result(self): """Return a searchable pandas dataframe of results from the last query. :return: """ return self._last_query_results.copy(deep=True)